<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Florent Clairambault &#187; C++</title>
	<atom:link href="http://florent.clairambault.fr/tag/c/feed" rel="self" type="application/rss+xml" />
	<link>http://florent.clairambault.fr</link>
	<description></description>
	<lastBuildDate>Sat, 04 Feb 2012 19:56:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19719</generator>
		<item>
		<title>Stupid C++ vs C# performance comparison</title>
		<link>http://florent.clairambault.fr/stupid-cpp-vs-charp-performance-comparison?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=stupid-cpp-vs-charp-performance-comparison</link>
		<comments>http://florent.clairambault.fr/stupid-cpp-vs-charp-performance-comparison#comments</comments>
		<pubDate>Fri, 26 Feb 2010 20:00:35 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[g++]]></category>
		<category><![CDATA[gmcs]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mono]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2409</guid>
		<description><![CDATA[I found out that there is real test than the little one i did here. In fact the main interest of the post is this spreadsheet : I recently did a simple and stupid comparison test between C++ (g++) and C# .Net (mono). My point here is that C# can actually be considered as a [...]]]></description>
			<content:encoded><![CDATA[<p>I found out that there is <a href="http://reverseblade.blogspot.com/2009/02/c-versus-c-versus-java-performance.html">real test</a> than the little one i did here. In fact the main interest of the post is this spreadsheet :</p>
<div style="text-align: center">
<a href="http://i.mivi.fr/013092c429a64dd39a55850d8467bad0869e5b35.gif"><img src="http://i.mivi.fr/013092c429a64dd39a55850d8467bad0869e5b35.x.700.gif" /></a>
</div>
<p>I recently did a simple and stupid comparison test between C++ (g++) and C# .Net (mono).</p>
<p>My point here is that C# can actually be considered as a very fast language. It allows automatic hardware-specific optimization. The only real drawback you have in the .Net framework is the garbage collector. It prevents the C# .Net it from being a realtime language/framework. But does it really matter for you ?</p>
<p>The purpose of the following test is to show that C++ isn&#8217;t faster than C# &#8220;as is&#8221;. I KNOW C# can not be faster than C++ because C# can do few hardware specific optimizations when you can do all of them in C++, because all the systems calls have to be done in C/C++ and because there&#8217;s no such thing like Template and forced inlining in C#.</p>
<p>But if you&#8217;re searching for arguments to choose C# .Net over native C++, you should also consider these :</p>
<ul>
<li>C# is much more simple. No headers, the languages specifications are simple and clear</li>
<li>You don&#8217;t have to manage the memory in .Net (no memory leak, optimal memory consumption, etc.)</li>
<li>C# code is as portable as java, it doesn&#8217;t need to be manually ported to a new architecture like C++</li>
<li>.Net don&#8217;t just crash, they always throw an exception and they only crash if nobody catches it</li>
<li>Mono (and presumely the .Net framework) does some really interesting optimizations :

<div class="wp_codebox"><table><tr id="p240910"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2409code10"><pre class="sh" style="font-family:monospace;"># mono --list-opt</pre></td></tr></table></div>

<p>I put some link so that you can learn about these really interesting optimization. This is like design patterns, you should really know these things.</em></p>
<table>
<tr>
<td>peephole</td>
<td><a href="http://en.wikipedia.org/wiki/Peephole_optimization">Peephole postpass</a></td>
</tr>
<tr>
<td>branch</td>
<td><a href="http://www.compileroptimizations.com/category/branch_optimization.htm">Branch optimizations</a></td>
</tr>
<tr>
<td>inline</td>
<td><a href="http://dotnetperls.com/inline-optimization">Inline method calls</a></td>
</tr>
<tr>
<td>cfold</td>
<td><a href="http://www.nullstone.com/htmls/category/consfold.htm">Constant folding</a></td>
</tr>
<tr>
<td>consprop</td>
<td><a href="http://en.wikipedia.org/wiki/Constant_folding#Constant_propagation">Constant propagation</a></td>
</tr>
<tr>
<td>copyprop</td>
<td><a href="http://en.wikipedia.org/wiki/Copy_propagation">Copy propagation</a></td>
</tr>
<tr>
<td>deadce</td>
<td><a href="http://en.wikipedia.org/wiki/Dead_code_elimination">Dead code elimination</a></td>
</tr>
<tr>
<td>linears</td>
<td><a href="http://en.wikipedia.org/wiki/Register_allocation">Linear scan global reg allocation</a></td>
</tr>
<tr>
<td>cmov</td>
<td><a href="http://assemblyrequired.crashworks.org/2009/01/04/fcmp-conditional-moves-for-branchless-math/">Conditional moves</a></td>
</tr>
<tr>
<td>shared</td>
<td>Emit per-domain code</td>
</tr>
<tr>
<td>sched</td>
<td><a href="http://en.wikipedia.org/wiki/Instruction_scheduling">Instruction scheduling</a></td>
</tr>
<tr>
<td>intrins</td>
<td>Intrinsic method implementations</td>
</tr>
<tr>
<td>tailc</td>
<td><a href="http://en.wikipedia.org/wiki/Tail_recursion">Tail recursion and tail calls</a></td>
</tr>
<tr>
<td>loop</td>
<td><a href="http://en.wikipedia.org/wiki/Loop_optimization">Loop related optimizations</a></td>
</tr>
<tr>
<td>fcmov</td>
<td><a href="http://en.wikipedia.org/wiki/FCMOV">Fast x86 FP compares</a></td>
</tr>
<tr>
<td>leaf</td>
<td><a href="http://moby.cs.uchicago.edu/documentation/HTML/ImplNotes/report011.html#htoc82">Leaf procedures optimizations</a></td>
</tr>
<tr>
<td>aot</td>
<td><a href="http://www.mono-project.com/AOT">Usage of Ahead Of Time compiled code</a></td>
</tr>
<tr>
<td>precomp</td>
<td>Precompile all methods before executing Main</td>
</tr>
<tr>
<td>abcrem</td>
<td><a href="http://blogs.msdn.com/clrcodegeneration/archive/2009/08/13/array-bounds-check-elimination-in-the-clr.aspx">Array bound checks removal</a></td>
</tr>
<tr>
<td>ssapre</td>
<td><a href="http://en.wikipedia.org/wiki/Partial_redundancy_elimination">SSA based Partial Redundancy Elimination</a></td>
</tr>
<tr>
<td>exception</td>
<td><a href="http://stackoverflow.com/questions/1108050/does-the-net-jit-optimize-nested-try-catch-statements">Optimize exception catch blocks</a></td>
</tr>
<tr>
<td>ssa</td>
<td><a href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Use plain SSA form</a></td>
</tr>
<tr>
<td>sse2</td>
<td><a href="http://en.wikipedia.org/wiki/SSE2">SSE2 instructions on x86</a></td>
</tr>
<tr>
<td>gshared</td>
<td>Share generics</td>
</tr>
<tr>
<td>simd</td>
<td><a href="http://en.wikipedia.org/wiki/SIMD">Simd intrinsics</a></td>
</tr>
</table>
</li>
<li>Multithreading in .net is much easier than any other language. You even have the parrallel loop operations, like <a href="http://www.lovethedot.net/2009/02/parallelfor-deeper-dive-parallel.html">Parallel.For</a>.</li>
</ul>
<p><strong>SO ! Here is the test&#8230;</strong></p>
<p>I wrote these two sample programs :</p>
<p>One in C++ :</p>

<div class="wp_codebox"><table><tr id="p240911"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p2409code11"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">long</span> stupidThing<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">long</span> nb <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">long</span> out <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> nb <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>
		out <span style="color: #000040;">*</span><span style="color: #000080;">=</span> nb<span style="color: #000040;">--</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
	<span style="color: #0000ff;">return</span> out<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #0000ff;">long</span> total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">1000000</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i <span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">long</span> l <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> l <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">100</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>l <span style="color: #008000;">&#41;</span>
			total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> stupidThing<span style="color: #008000;">&#40;</span> l <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Total : &quot;</span> <span style="color: #000080;">&lt;&lt;</span> total <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>One in C# :</p>

<div class="wp_codebox"><table><tr id="p240912"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p2409code12"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> test <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">class</span> Program <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">long</span> stupidThing<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">long</span> nb <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                        <span style="color: #6666cc; font-weight: bold;">long</span> ret <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                        <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span> nb <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span>
                                ret <span style="color: #008000;">*=</span> nb<span style="color: #008000;">--;</span>
                        <span style="color: #0600FF; font-weight: bold;">return</span> ret<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
                        <span style="color: #6666cc; font-weight: bold;">long</span> total <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
                        <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1000000</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i <span style="color: #008000;">&#41;</span>
                                <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">long</span> l <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> l <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>l <span style="color: #008000;">&#41;</span>
                                        total <span style="color: #008000;">+=</span> stupidThing<span style="color: #008000;">&#40;</span> l <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Total : {0}&quot;</span>, total <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>First of all, I open a shell in real-time priority, because I don&#8217;t want my other processses to mess with my tests :</p>

<div class="wp_codebox"><table><tr id="p240913"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2409code13"><pre class="sh" style="font-family:monospace;"># rtprio 99 bash</pre></td></tr></table></div>

<p>Then I compile the two programs :</p>

<div class="wp_codebox"><table><tr id="p240914"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2409code14"><pre class="sh" style="font-family:monospace;"># gmcs test.cs
# g++ -O4 test.cpp -o test</pre></td></tr></table></div>

<p>And then I launch my test :</p>
<p>On a 64 bits host :</p>

<div class="wp_codebox"><table><tr id="p240915"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2409code15"><pre class="txt" style="font-family:monospace;">Kernel : 2.6.9-023stab051.3-smp #1 SMP Wed Nov 4 18:36:34 MSK 2009 x86_64 x86_64 x86_64 GNU/Linux</pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p240916"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p2409code16"><pre class="sh" style="font-family:monospace;"># time ./test ; time ./test ; time ./test ; time mono test.exe ; time mono test.exe ; time mono test.exe
Total : -6192109806162068864
&nbsp;
real    0m12.433s
user    0m12.394s
sys     0m0.049s
Total : -6192109806162068864
&nbsp;
real    0m12.415s
user    0m12.411s
sys     0m0.014s
Total : -6192109806162068864
&nbsp;
real    0m12.430s
user    0m12.411s
sys     0m0.026s
Total : -6192109806162068864
&nbsp;
real    0m10.311s
user    0m10.287s
sys     0m0.029s
Total : -6192109806162068864
&nbsp;
real    0m10.254s
user    0m10.247s
sys     0m0.011s
Total : -6192109806162068864
&nbsp;
real    0m10.250s
user    0m10.255s
sys     0m0.012s</pre></td></tr></table></div>

<p>C# clearly beats C++ here. Well</p>
<p>On a 32 bits host :</p>

<div class="wp_codebox"><table><tr id="p240917"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2409code17"><pre class="txt" style="font-family:monospace;">Kernel : 2.6.30-2-686 #1 SMP Fri Dec 4 00:53:20 UTC 2009 i686 GNU/Linux</pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p240918"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p2409code18"><pre class="sh" style="font-family:monospace;"># time ./test ; time ./test ; time ./test ; time mono test.exe ; time mono test.exe ; time mono test.exe
Total : 100461056
&nbsp;
real    1m10.927s
user    1m7.376s
sys     0m0.056s
Total : 100461056
&nbsp;
real    1m12.590s
user    1m8.976s
sys     0m0.020s
Total : 100461056
&nbsp;
real    1m13.279s
user    1m9.532s
sys     0m0.056s
Total : -6192109806162068864
&nbsp;
real    2m22.492s
user    2m15.260s
sys     0m0.136s
Total : -6192109806162068864
&nbsp;
real    2m23.002s
user    2m15.760s
sys     0m0.104s
Total : -6192109806162068864
&nbsp;
real    2m25.102s
user    2m17.709s
sys     0m0.144s</pre></td></tr></table></div>

<p>C++ beats C# here, but in 32 bits C++ use other types whereas C# use the same. In C# long is always 64 bits, in C++ it can be 64 bits or 32 bits (depending on the current architecture).</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/stupid-cpp-vs-charp-performance-comparison/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 1/13 queries in 0.004 seconds using disk: basic
Object Caching 660/672 objects using disk: basic

Served from: florent.clairambault.fr @ 2012-02-07 09:56:58 -->
