Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is .NET faster than C++ in this case?
    text
    copied!<p><strong>Make sure you run outside of the IDE.</strong> That is key.</p> <p>-edit- I LOVE SLaks comment. "The amount of misinformation in these answers is staggering." :D</p> <p>Calm down guys. Pretty much all of you were wrong. I DID make optimizations. <strike>It turns out whatever optimizations I made wasn't good enough.</strike> I ran the code in GCC using gettimeofday (I'll paste code below) and used <code>g++ -O2 file.cpp</code> and got slightly faster results then C#. <strike>Maybe MS didn't create the optimizations needed in this specific case but after downloading and installing mingw I was tested and found the speed to be near identical.</strike> <a href="https://stackoverflow.com/questions/2285864/why-is-net-faster-than-c-in-this-case/2286084#2286084">Justicle</a> Seems to be right. I could have sworn I use clock on my PC and used that to count and found it was slower but problem solved. C++ speed isn't almost twice as slower in the MS compiler.</p> <p>When my friend informed me of this I couldn't believe it. So I took his code and put some timers onto it.</p> <p>Instead of <a href="http://en.wikipedia.org/wiki/Boo_%28programming_language%29" rel="nofollow noreferrer">Boo</a> I used C#. I constantly got faster results in C#. Why? The .NET version was nearly half the time no matter what number I used.</p> <p>C++ version (bad version):</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;intrin.h&gt; #include &lt;windows.h&gt; using namespace std; int fib(int n) { if (n &lt; 2) return n; return fib(n - 1) + fib(n - 2); } int main() { __int64 time = 0xFFFFFFFF; while (1) { int n; //cin &gt;&gt; n; n = 41; if (n &lt; 0) break; __int64 start = __rdtsc(); int res = fib(n); __int64 end = __rdtsc(); cout &lt;&lt; res &lt;&lt; endl; cout &lt;&lt; (float)(end-start)/1000000&lt;&lt;endl; break; } return 0; } </code></pre> <p>C++ version (better version):</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;intrin.h&gt; #include &lt;windows.h&gt; using namespace std; int fib(int n) { if (n &lt; 2) return n; return fib(n - 1) + fib(n - 2); } int main() { __int64 time = 0xFFFFFFFF; while (1) { int n; //cin &gt;&gt; n; n = 41; if (n &lt; 0) break; LARGE_INTEGER start, end, delta, freq; ::QueryPerformanceFrequency( &amp;freq ); ::QueryPerformanceCounter( &amp;start ); int res = fib(n); ::QueryPerformanceCounter( &amp;end ); delta.QuadPart = end.QuadPart - start.QuadPart; cout &lt;&lt; res &lt;&lt; endl; cout &lt;&lt; ( delta.QuadPart * 1000 ) / freq.QuadPart &lt;&lt;endl; break; } return 0; } </code></pre> <p>C# version:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.ComponentModel; using System.Threading; using System.IO; using System.Diagnostics; namespace fibCSTest { class Program { static int fib(int n) { if (n &lt; 2)return n; return fib(n - 1) + fib(n - 2); } static void Main(string[] args) { //var sw = new Stopwatch(); //var timer = new PAB.HiPerfTimer(); var timer = new Stopwatch(); while (true) { int n; //cin &gt;&gt; n; n = 41; if (n &lt; 0) break; timer.Start(); int res = fib(n); timer.Stop(); Console.WriteLine(res); Console.WriteLine(timer.ElapsedMilliseconds); break; } } } } </code></pre> <p>GCC version:</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;sys/time.h&gt; using namespace std; int fib(int n) { if (n &lt; 2) return n; return fib(n - 1) + fib(n - 2); } int main() { timeval start, end; while (1) { int n; //cin &gt;&gt; n; n = 41; if (n &lt; 0) break; gettimeofday(&amp;start, 0); int res = fib(n); gettimeofday(&amp;end, 0); int sec = end.tv_sec - start.tv_sec; int usec = end.tv_usec - start.tv_usec; cout &lt;&lt; res &lt;&lt; endl; cout &lt;&lt; sec &lt;&lt; " " &lt;&lt; usec &lt;&lt;endl; break; } return 0; } </code></pre>
 

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