Note that there are some explanatory texts on larger screens.

plurals
  1. POC# vs. C++ performance -- why doesn't .NET perform the most basic optimizations (like dead code elimination)?
    primarykey
    data
    text
    <p>I'm seriously doubting if the C# or .NET JIT compilers perform <em>any</em> useful optimizations, much less if they're actually competitive with the most basic ones in C++ compilers.</p> <p>Consider this extremely simple program, which I conveniently made to be valid in both C++ and C#:</p> <pre><code>#if __cplusplus #else static class Program { #endif static void Rem() { for (int i = 0; i &lt; 1 &lt;&lt; 30; i++) ; } #if __cplusplus int main() #else static void Main() #endif { for (int i = 0; i &lt; 1 &lt;&lt; 30; i++) Rem(); } #if __cplusplus #else } #endif </code></pre> <p>When I compile and run it in the newest version of C# (VS 2013) in release mode, it doesn't terminate in any reasonable amount of time.</p> <p><strong>Edit</strong>: Here's another example:</p> <pre><code>static class Program { private static void Test2() { } private static void Test1() { #if TEST Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); Test2(); #else Test2(); #endif } static void Main() { for (int i = 0; i &lt; 0x7FFFFFFF; i++) Test1(); } } </code></pre> <p>When I run this one, it takes a <strong>lot</strong> longer if <code>TEST</code> is defined, even though everything is a no-op and <code>Test2</code> should be inlined.</p> <p><em>Even the the most ancient C++ compilers</em> I can get my hands on, however, optimize everything away, making the programs return immediately.</p> <h3>What prevents the .NET JIT optimizer from being able to make such simple optimizations? Why?</h3>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload