Why is my application startup time slower when I use Process.Start()?
Asked Answered
G

3

6

I'm trying to profile the startup time of my application, so I wrote a small C# program that will start my application using the Process.Start() method, and time it using a stopwatch.

When I try to start the application myself (by just clicking on it), it probably takes 2-3 seconds. When I try to start the application using my test program, it takes 8-10 seconds. The startup time consistently differs in that magnitude.

Any idea why using Process.Start to start an executable would affect startup times so much?

Gensmer answered 27/8, 2010 at 1:33 Comment(2)
are you running it under the debugger? Or in Release mode? Are you starting it in visual studio or on the command line?Spinet
I'm running it in Release mode, and from the command-lineGensmer
G
1

Thanks for all your help. I have the answer, and it's unrelated to Process.Start.

After I start the process, I was waiting for a specific window handle to appear to know that the app actually showed up. The loop was too tight. I introduced a 200 ms sleep in the while loop, and startup time was normal again.

Gensmer answered 28/8, 2010 at 7:17 Comment(1)
dreadpirateriyan, please select this as answer, even if it is from yourself to help others find this faster.Nelle
F
0

Your clue should be that Process.Start() is in the System.Diagnostics namespace. When you start a process in this manner, you're attaching a bunch of monitors/inspectors to it. This definitely adds an overhead.

You might want to try immediately calling Dispose() on the Process object after you start it (in order to avoid unnecessarily-prolonged process monitoring), but you will not be able to completely avoid the associated overheads.

Fanatic answered 27/8, 2010 at 1:54 Comment(1)
I don't see anything in the documentation about use of Process.Start resulting in slower runtime performance for the spawned application. Do you have any links? I would be interested in reading up on that.Mckinley
S
0

Simply put, you are starting actually two process's not just one. That's why it takes longer.

When you double click on your app, you are loading just one application and all it's DLL's.

When you run your diagnostic app, you first are loading the first application with it's .NET assemblies which have to be JIT'd (Just in time compilation: which is not free). Only then after that is all done, then the OTHER application gets to start. If your target app is also a .NET app, then the whole cycle repeats itself.

Spinet answered 27/8, 2010 at 2:13 Comment(1)
He's timing it with a Stopwatch, so presumably he's measuring only the startup time of the Process, and not the startup time of his test app.Mckinley

© 2022 - 2024 — McMap. All rights reserved.