Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't get even a simple method to inline (?)
    primarykey
    data
    text
    <p>Logically, I always assumed that very short and simple methods would get inlined by the C# Compiler and thus won't show any overhead compared to simply typing the code in the methods by hand...</p> <p>Until today - when I tried to benchmark various methods and manually inlined code. Turns out that (for me) even the most simple code will show a method call overhead compared to its manually inlined counterpart.<br/> Actually, I could find no clues as to <em>any</em> method being inlined - So I ran a simple test.</p> <p>System used:</p> <ul> <li>Intel C2D E7200 (Dual Core) 2.53GHz</li> <li>4GB DDR2</li> <li>Windows 7 64bit</li> <li>.NET 4.0</li> <li>Visual Studio 2010 Ultimate</li> </ul> <p>All tests were performed <strong>without Debug</strong> and using <strong>Release Configuration (Optimize Code)</strong>.</p> <p>Here's the code I used to benchmark:</p> <pre><code>static void Main() { const int iterations = 250000000; // 250 million iterations Thread.Sleep(1000); // sleep for one second var sw = new Stopwatch(); int s = 0; sw.Start(); for (int i = 0; i &lt; iterations; i++) { // incrementing s by 1 in various ways } sw.Stop(); Console.WriteLine("Time: {0}ms", sw.ElapsedMilliseconds); } </code></pre> <p><strong>[1]</strong> First, I've simply benchmarked a simple increment command:</p> <pre><code>// in Main for (int i = 0; i &lt; iterations; i++) { s = s + 1; } </code></pre> <p>Results from 5 runs:</p> <ol> <li>867ms</li> <li>877ms</li> <li>868ms</li> <li>865ms</li> <li>870ms</li> </ol> <hr> <p><strong>[2]</strong> Switching to a method call:</p> <pre><code>static int Increment(int a) { return a + 1; } </code></pre> <p>...</p> <pre><code>// in Main for (int i = 0; i &lt; iterations; i++) { s = Increment(s); } </code></pre> <p>Results from 5 runs:</p> <ol> <li>2161ms</li> <li>2159ms</li> <li>2194ms</li> <li>2177ms</li> <li>2163ms</li> </ol> <p>Ouch! Obviously there's an overhead for the method. </p> <p>I've tried to use reflection, and printed <code>MethodBase.GetCurrentMethod().Name</code> from within the <code>Increment</code> method; it was indeed printing <code>Increment</code> - meaning the method isn't inlined.</p> <p>Next I've tried to add the <code>[MethodImpl(MethodImplOptions.NoInlining)]</code> attribute to the method - <strong>but the benchmark time stayed exactly the same</strong>.</p> <p>In Debug mode and with Optimize Code set to false, the first test is slightly slower while the second one is about two times slower; and yet again the <em>NoInlining</em> attribute doesn't impact performance.</p> <hr> <p><strong>Am I doing something wrong here that I fail to make even such a simple method work without an overhead? Why is this happening?</strong><br/> Surely this can't be expected behavior - or is it?</p> <p><em>Note: Similar testing in Java shows no overhead to such a method call. (using Eclipse + JDK 1.7, Java also seems to be <strong>A LOT</strong> faster at this.)</em></p>
    singulars
    1. This table or related slice is empty.
    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.
    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