× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

haskell processing so much faster than C# (codeeval)

Carsten König
Jul 17, 2015
<p>for C# it is your <code>Console.WriteLine</code>:</p> <p>this one:</p> <pre><code> var timer = new Stopwatch(); timer.Start(); var sb = new StringBuilder(); for (int i = 1; i &lt;= 99; i++) { if (i%2 == 1) sb.AppendLine(i.ToString()); } Console.Write(sb.ToString()); timer.Stop(); Console.WriteLine("took {0:0.0}ms", timer.Elapsed.TotalMilliseconds); </code></pre> <p>takes 1-2ms on in release on my machine, yours take around 10ms</p> <p>For their site it might take even longer, depending on what they do with the output.</p> <p>Aside from this C#'s generated code will still be interpreted by some runtime, so you might even measure the initialization/startup as well </p> <p>Haskell might probably not (although they might just <code>runhaskell</code> as well ...(?))</p> <p>I guess you have to ask the support from CodeEval for details.</p> <hr> <h3>LINQ style</h3> <p>back to around 10ms but closer to Haskell</p> <pre><code> var numbers = String.Join("\n", Enumerable .Range(1, 99) .Where(i =&gt; i%2 == 1) .Select(i =&gt; i.ToString())); Console.WriteLine(numbers); </code></pre> <hr> <h3>some fun / performance tests</h3> <p>I extended the range to 9999 with this in <em>eshell</em> <code>time ./prg.exe</code> (yeah I'm on windows right now :( ):</p> <ul> <li>your original Haskell version with <code>ghc -O2</code>: about <strong>0.72s</strong></li> <li>the <code>Stringbuilder</code> version I posted above compiled to release: around <strong>0.87</strong></li> </ul> <p>quite close <em>IMO</em></p> <p>here with 99999:</p> <ul> <li>C# <strong>8.2s</strong></li> <li>Haskell: <strong>8.2s</strong></li> </ul> <p>so no worry they seem to be very close ;)</p> <p>But I am not surprised at all - in the end all you do is print something to the console - so it should only depend on your OS/machine.</p> <p>The other problems might be more interesting but I am to lazy to write code for them</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31160546/haskell%20processing%20so%20much%20faster%20than%20C#%20(codeeval)/31160864">Stack Overflow</a>.</p>
comments powered by Disqus