Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A few minutes of work and you can benchmark this yourself reasonably enough to see the difference.</p> <p><strong>1.) Create a general purpose timing routine.</strong></p> <pre><code> public void RunTest(Action action, String name, Int32 iterations) { var sw = new System.Diagnostics.Stopwatch(); System.GC.Collect(); System.GC.WaitForPendingFinalizers(); sw.Start(); for(var i = 0; i&lt;iterations; i++) { action(); } sw.Stop(); Console.Write("Name: {0},", name); Console.Write(" Iterations: {1},", iterations); Console.WriteLine(" Milliseconds: {2}", sw.ElapsedMilliseconds); } </code></pre> <p><strong>2.) Create your two test functions.</strong></p> <pre><code> public void CompareWithParseInt() { int a = 1; string b = "1"; bool isEqual = a == int.Parse(b); } public void CompareWithToString() { int a = 1; string b = "1"; bool isEqual = a.ToString() == b; } </code></pre> <p><strong>3.) Now benchmark the crap out of them.</strong></p> <pre><code>RunTest(CompareWithParseInt, "Compare With ParseInt", 1); //Name: Compare With ParseInt, Iterations: 1, Milliseconds: 0 RunTest(CompareWithParseInt, "Compare With ParseInt", 1); //Name: Compare With ParseInt, Iterations: 1, Milliseconds: 0 RunTest(CompareWithParseInt, "Compare With ParseInt", 10); //Name: Compare With ParseInt, Iterations: 10, Milliseconds: 0 RunTest(CompareWithParseInt, "Compare With ParseInt", 100); //Name: Compare With ParseInt, Iterations: 100, Milliseconds: 0 RunTest(CompareWithParseInt, "Compare With ParseInt", 1000); //Name: Compare With ParseInt, Iterations: 1000, Milliseconds: 0 RunTest(CompareWithParseInt, "Compare With ParseInt", 100000); //Name: Compare With ParseInt, Iterations: 100000, Milliseconds: 12 RunTest(CompareWithParseInt, "Compare With ParseInt", 1000000); //Name: Compare With ParseInt, Iterations: 1000000, Milliseconds: 133 RunTest(CompareWithToString, "Compare With ToString", 1000000); //Name: Compare With ToString, Iterations: 1000000, Milliseconds: 112 RunTest(CompareWithToString, "Compare With ToString", 100000000); //Name: Compare With ToString, Iterations: 100000000, Milliseconds: 10116 RunTest(CompareWithParseInt, "Compare With ParseInt", 100000000); //Name: Compare With ParseInt, Iterations: 100000000, Milliseconds: 12447 </code></pre> <p><strong>Conclusion:</strong></p> <p>As you can see in this particular case, you can pretty much do whatever makes you happy because if there is a bottleneck in your application. It's not with the above code :)</p>
 

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