I was working on a project today, and found myself using Math.Max in several places and inline if statements in other places. So, I was wondering if anybody knew which is "better"... or rather, what the real differences are.
For example, in the following, c1 = c2
:
Random rand = new Random(); int a = rand.next(0,10000); int b = rand.next(0,10000); int c1 = Math.Max(a, b); int c2 = a>b ? a : b;
I'm asking specifically about C#, but I suppose the answer could be different in different languages, though I'm not sure which ones have similar concepts.
when you execute Math. min(), you will get (Infinity) and when you execute Math. max(), you will get (-Infinity). by this result for sure if you compare Math.
max method is likely to be slower, but it also may return a different result if one of the arguments is NaN.
Using Math. Min in a loop is almost 4 times slower than before.
One of the major differences I would notice right away would be for readability sake, as far as I know for implementation/performance sake, they would be nearly equivalent.
Math.Max(a,b)
is very simple to understand, regardless of previous coding knowledge.
a>b ? a : b
would require the user to have some knowledge of the ternary operator, at least.
"When in doubt - go for readability"
I thought it would be fun to throw in some numbers into this discussion so I wrote some code to profile it. As expected they are almost identical for all practical purposes.
The code does a billion loops (yep 1 billion). Subtracting the overhead of the loop you get:
I subtracted the overhead which I calculated by running an empty loop 1 billion times, the overhead was 1.2 seconds.
I ran this on a laptop, 64-bit Windows 7, 1.3 Ghz Intel Core i5 (U470). The code was compiled in release mode and ran without a debugger attached.
Here's the code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace TestMathMax { class Program { static int Main(string[] args) { var num1 = 10; var num2 = 100; var maxValue = 0; var LoopCount = 1000000000; double controlTotalSeconds; { var stopwatch = new Stopwatch(); stopwatch.Start(); for (var i = 0; i < LoopCount; i++) { // do nothing } stopwatch.Stop(); controlTotalSeconds = stopwatch.Elapsed.TotalSeconds; Console.WriteLine("Control - Empty Loop - " + controlTotalSeconds + " seconds"); } Console.WriteLine(); { var stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < LoopCount; i++) { maxValue = Math.Max(num1, num2); } stopwatch.Stop(); Console.WriteLine("Math.Max() - " + stopwatch.Elapsed.TotalSeconds + " seconds"); Console.WriteLine("Relative: " + (stopwatch.Elapsed.TotalSeconds - controlTotalSeconds) + " seconds"); } Console.WriteLine(); { var stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < LoopCount; i++) { maxValue = num1 > num2 ? num1 : num2; } stopwatch.Stop(); Console.WriteLine("Inline Max: " + stopwatch.Elapsed.TotalSeconds + " seconds"); Console.WriteLine("Relative: " + (stopwatch.Elapsed.TotalSeconds - controlTotalSeconds) + " seconds"); } Console.ReadLine(); return maxValue; } } }
UPDATED Results 2/7/2015
On a Windows 8.1, Surface 3 Pro, i7 4650U 2.3Ghz Ran as a console application in release mode without the debugger attached.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With