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!
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With