title: How to enable optimization locally in Debug mode?
body: <p>The following piece of code takes ~700 ms to run in Debug mode (Visual Studio 2017). I have read numerous questions on how to speed it up, but the majority of things I tried does not seem to have any effect.</p>
  <p>By bisecting compiler flags, I found that the main slowdowns result from from <code>/RTC1</code>/<code>/RTCs</code> (vs. <code>/RTCu</code>) and from <code>/Od</code> (vs. <code>/O2</code>). (I previously stated in the comments that they did not have a big effect, but I had been running the Debug/x86 configuration while modifying the Debug/x64 configuration...) <code>_ITERATOR_DEBUG_LEVEL</code> has a measurable, but negligible effect, and I don't worry too much about the remaining difference to Release mode.</p>
  <p>My aim is to modify just a single piece of code (ideally, a function with just the std::equal call) that is not critical for routine debugging. I have been able to bring runtimes down &quot;locally&quot; (without changing global project options) by <code>#pragma runtime_checks</code>, basically countering <code>/RTCs</code>; but the same won't work for <code>#pragma optimize</code>, it seems.</p>
  <pre class="lang-cpp prettyprint-override"><code>// Debug (Default, /Od and /RTC1): 700 ms
  // Debug (/Od /RTCs): 700 ms
  // Debug (/Od /RTCu): 200 ms
  // Debug (/Od /RTC1 with #pragma runtime_checks at main): 440 ms
  // Debug (/Od /RTC1 with #pragma runtime_checks at top): 220 ms
  // Debug (/Od): 200 ms
  // Debug (/O2): 60 ms
  // Debug (/O2 with _ITERATOR_DEBUG_LEVEL 0): 50 ms
  // Release: 20 ms
  
  #pragma runtime_checks( &quot;s&quot;, off )
  #pragma optimize( &quot;gsy&quot;, on )
  #define _ITERATOR_DEBUG_LEVEL 0
  
  #include &lt;algorithm&gt;
  #include &lt;chrono&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  using clk = chrono::steady_clock;
  
  // #pragma runtime_checks( &quot;s&quot;, off )
  int main()
  {
    const ifstream is(R&quot;(C:\Windows\explorer.exe)&quot;, ios::binary);
  
    auto begin = clk::now();
    equal(
      istreambuf_iterator&lt;char&gt;(is.rdbuf()),
      istreambuf_iterator&lt;char&gt;(),
      istreambuf_iterator&lt;char&gt;(is.rdbuf())
    );
    auto end = clk::now();
  
    cout &lt;&lt; chrono::duration_cast&lt;chrono::milliseconds&gt;(end - begin).count();
    cout &lt;&lt; &quot; ms&quot; &lt;&lt; endl;
  }
  </code></pre>
  <p>The documentation for <code>#pragma runtime_checks</code> says</p>
  <blockquote>
  <p>You can't enable a run-time check that wasn't enabled by a compiler option.</p>
  </blockquote>
  <p>I suspect something similar holds for <code>#pragma optimize</code>, in the sense that you cannot enable optimization that wasn't enabled by a compiler option. Could this be true? Is there anything else I can try to enforce optimization locally (apart from putting the function in its own file and configuring compiler options in the properties, which does work)?</p>