Launching VS Profiler boosts Application Performance x20?

后端 未结 4 1226
情深已故
情深已故 2021-02-03 14:01

EDIT 1
I do not exclude at all this might be caused by something very basic side effect of using the Profiler (some faulty setting in my \"regular\" pro

相关标签:
4条回答
  • 2021-02-03 14:34

    If you are using threads within your code, the windows timer resolution is also a reason for this.

    Default windows timer resolution is 15.6ms

    When you run your application with the profiler, this is set to 1ms and the application runs faster. Checkout this answer

    0 讨论(0)
  • 2021-02-03 14:38

    Running with the debugger disables jit optimizations. If you run the exe normally jit optimizations will be enabled. Attaching a debugger to such a running application allows you to debug it with enabled optimizations.

    Release-Build vs Debug-Build has two consequences:

    1. A conditional compiler symbol is (un)defined
    2. It enables/disables optimizations in the C# => IL compilation.
    0 讨论(0)
  • 2021-02-03 14:41

    I've come across this many times... There is a very simple explanation for it. The profiler doesn't dispose objects, so the cost of object-disposal is not incurred while profiling.

    Thus, if you want to improve performance to make it match the profiled performance, figure out where you're instantiating all these short-lived objects, and refactor the code.

    I don't yet know of a really great way to immediately find the offending code, but I can help you narrow it down. If you profile your code, open the report, select "Functions" as your Current View, and then sort by Inclusive Samples, you will see the top methods... Your performance problem with object-instantiations is likely to be in one of those methods with the most Inclusive Samples.

    0 讨论(0)
  • 2021-02-03 14:54

    Considering that the question is about code running faster under the profiler, and that the specific questions are "1. WHAT EXACTLY HAPPENS, HOW, and WHY ? and 2. How to make my code behave like that natively ?" the accepted answer is wrong, since it does not address the profiler at all, nor does it mention the specific cause of the 20x speed up.

    What exactly happens is this:

    In the "Debug" tab of your project properties the "Enable unmanaged code debugging" check box is checked; this option causes the debugger to be slow as molasses.

    When you run the profiler, it is still the debug version of your program which you are profiling, so the "DEBUG" symbol is defined, and all the tracing, assertions, etc. are enabled, but the debugger is not involved, so the "Enable unmanaged code debugging" option is not applicable. (JIT optimizations are probably enabled while profiling, but that would not account for even a 2x performance boost, let alone a 20x boost.)

    If you want to enjoy this 20x boost while debugging your code, (not only while profiling,) go to the "Debug" tab of your project properties and make sure that "Enable unmanaged code debugging" is unchecked.

    P.S.

    There is a question which is a few-month-older duplicate of this one: How Come when I sampling profile a program and it actually runs faster than not profiling?

    0 讨论(0)
提交回复
热议问题