Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math.Max vs inline if - what are the differences?

Tags:

c#

inline-if

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.

like image 462
chezy525 Avatar asked Mar 29 '11 21:03

chezy525


People also ask

What is the difference between math MIN () and math MAX () functions?

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.

Is Max math slower?

max method is likely to be slower, but it also may return a different result if one of the arguments is NaN.

Is math min slow?

Using Math. Min in a loop is almost 4 times slower than before.


2 Answers

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"

like image 97
Rion Williams Avatar answered Sep 21 '22 03:09

Rion Williams


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:

  • Math.Max() took .0044 seconds to run 1 billion times
  • The inline if took .0055 seconds to run 1 billion times

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.

  • Math.Max() - 0.3194749 seconds
  • Inline Max: 0.3465041 seconds
like image 30
Luis Perez Avatar answered Sep 21 '22 03:09

Luis Perez