Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application runs faster with visual studio performance analysis

Tags:

I am investigating for how many time it takes for a particular operation to complete. The operation is like the following:

Parallel.ForEach(items, item => SaveScheme(item));

The SaveScheme method works with a database: executes some queries and works with the information. The amount of elements in items collection can be big enough.

When I run this operation, it takes about 20-40 seconds to complete. But when I run it with a profiling turned on, it takes only 3 seconds!

I didn't find any information about this problem. My only guess is that with profiling Parallel.ForEach creates more threads than without it, but I don't know for sure, and even if it's true, I don't know what to do with it.

So, why is that happens and how can I archieve this performance when I run the application without profiling?


UPD. Parallel has nothing to do with this: I tested with simple foreach instead and the operation still completes in 3 seconds!

like image 770
STiLeTT Avatar asked Aug 16 '12 09:08

STiLeTT


People also ask

What is performance profiler in Visual Studio?

Profiling and diagnostics tools help you diagnose memory and CPU usage and other application-level issues. With these tools, you can accumulate performance data while you run your application.

Does Visual Studio limit memory usage?

If you upgrade your system from a 32-bit version of Windows to a 64-bit version, you expand the amount of virtual memory available to Visual Studio from 2 GB to 4 GB. This enables Visual Studio to handle significantly larger workloads, even though it is 32-bit process.


2 Answers

I found the answer:

The reason is because when you run your application within Visual Studio, the debugger is attached to it. When you run it using the profiler, the debugger is not attached.

If you try running the .exe by itself, or running the program through the IDE with "Debug > Start Without Debugging" (or just press Ctrl+F5) the application should run as fast as it does with the profiler.

https://stackoverflow.com/a/6629040/1563172

I didn't find it earlier because I thought that the reason is concurrency.

like image 123
STiLeTT Avatar answered Sep 17 '22 17:09

STiLeTT


Since you are using threading in your program, the Windows timer resolution can also be a reason.

Default windows timer resolution is 15.6ms

When you run your application with the profiler, this is reduced to 1ms causing your application to run faster. Checkout this answer

like image 22
Kusal Dissanayake Avatar answered Sep 20 '22 17:09

Kusal Dissanayake