Math.Max/Min vs inline comparisons
This test stemmed from my having to write a lot of code, and look at a lot of code, that was doing comparisons between 2-3 numbers finding the Max and Min between them and then performing some mathematical calculations.
Naturally, throughout the code there were a lot of Math.Max(a,b), Math.Min(y,z), and even some with triplet comparisons:
- Math.Max(a,Math.Max(b,c))
- Math.Min(x,Math.Min(y,z))
Performance wise, I figured it would be faster to do my own inline comparisons, but by how much?
That’s where this Curious Consultant started wondering… how much slower is a simple Math.Max and Math.Min vs inline comparisons?
Let’s benchmark it and find out!
Constructing the Test
For this test, it’s straight forward:
- call a method with 3 ints and 3 doubles
- compare the max/min on 2 ints each way and then 2 doubles each way
- compare the max/min on 3 ints each way and then 3 doubles each way
- keep a running total just to verify the results are the same the max’s/min’s are chosen correctly
The code is written in Visual Studio 2022 targeting .Net Framework version 4.8 x64, compiled in “Release” mode the build option “Optimize Code” checked. The source code is available at the end of this blog so you can benchmark it on your own system.
The exe file was run under Windows 10 Professional 64-bit with 32GB of memory.
The test was run with each child method called the following number of times:
- 10,000
- 1,000,000
- 100,000,000
Ready to start!
These tests were run three times after a fresh boot with no other applications runrning to reduce the impact on the CPU. I’m only listing one of the result runs as they were all similar so I don’t feel a need to table-ize all 3.
All times are indicated in minutes.seconds.milliseconds format. Lower numbers indicate faster run time performance.
Winners are highlighted in green; there are no points for second place.
The Results of Math.Max/Min vs inline comparisons:
Test Run #1 (runs #2 and #3 had similar results)
|
Number of comparisons |
|||||
|
10,000 |
1,000,000 |
100,000,000 |
|||
Math.Max/Min w/ 2 ints |
00:00.0000498 |
00:00.0042790 |
00:00.4150294 |
|||
Inline w/ 2 ints |
00:00.0000416 |
00:00.0041887 |
00:00.3810809 |
|||
&nbsdp; | ||||||
Math.Max/Min w/ 2 doubles |
00:00.0001117 |
00:00.0043949 |
00:00.4273392 |
|||
Inline w/ 2 doubles |
00:00.0000402 |
00:00.0038357 |
00:00.3802753 |
|||
&nbsdp; | ||||||
Math.Max/Min w/ 3 ints |
00:00.0000472 |
00:00.0046687 |
00:00.4514242 |
|||
Inline w/ 3 ints |
00:00.0000404 |
00:00.0039352 |
00:00.3890031 |
|||
&nbsdp; | ||||||
Math.Max/Min w/ 3 doubles |
00:00.0000840 |
00:00.0079223 |
00:00.7739660 |
|||
Inline w/ 3 doubles |
00:00.0000580 |
00:00.0038909 |
00:00.3988726 |
In Summary:
Writing code inline is 99.9% of the time going to be faster than calling a static method. Thus, it’s no surprise in our Math.Max/Min vs inline comparisons that the static methods were slower.
What is amazing though is to see how much (significantly) faster it was to use our own inline comparison vs calling the static methods when comparing more than 2 double values. Even as the number of comparisons increased, the time ratio stayed about the same, which is 50%.
50% !!
Let that sink in.
Doing inline comparisons to find the highest value between 3 doubles is 50% faster than calling the static classes.
In practical terms, if you have one critical section of code that requires such a comparison, sure… write it once, be done with it, and rest assured that’s the fasted code going.
But if it has to be done in multiple locations?
Nobody is going to want to do this several times:
1 2 3 4 5 6 7 8 9 10 11 12 |
if (y > z) { if (y > w) sum += y; else if (y < w) sum += w; } else if (y < z) if (z > w) sum += z; else if (z < w) sum += w; |
instead of this:
1 |
sum += Math.Max(w, Math.Max(y,z)); |
What a coding maintenance nightmare!
As for this Curious Consultant, when speed is needed, this Math.Max/Min vs inline comparisons has shown that doing our own inline comparisons will be the fastest way to go.
Otherwise, code will be written in such a way that easy maintenance can be done.
Until next time!
The Code:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Concurrent; using System.Data; using System.Data.Sql; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Threading; using System.Diagnostics; namespace TestApplication { class Program { static void Main(string[] args) { DateTime end; DateTime start = DateTime.Now; Console.WriteLine("### Overall Start Time: " + start.ToLongTimeString()); Console.WriteLine(); TestMaxMinVsDoingItYourself(5, 8, 13, 5.0, 8.0, 13.0, 10000); TestMaxMinVsDoingItYourself(5, 8, 13, 5.0, 8.0, 13.0, 1000000); TestMaxMinVsDoingItYourself(5, 8, 13, 5.0, 8.0, 13.0, 100000000); end = DateTime.Now; Console.WriteLine(); Console.WriteLine("### Overall End Time: " + end.ToLongTimeString()); Console.WriteLine("### Overall Run Time: " + (end - start)); Console.WriteLine(); Console.WriteLine("Hit Enter to Exit"); Console.ReadLine(); } //#################################################### static void TestMaxMinVsDoingItYourself(int int1, int int2, int int3, double doub1, double doub2, double doub3, int numberOfLoops) { //Tests accessing the class property directly Console.WriteLine("######## " + System.Reflection.MethodBase.GetCurrentMethod().Name); Console.WriteLine("Number of loops: " + numberOfLoops.ToString("#,##0")); Stopwatch sw = new Stopwatch(); DateTime end = DateTime.Now; DateTime start = DateTime.Now; double sum = 0; Thread.Sleep(500); Console.WriteLine("###########################################################"); Console.WriteLine("Starting TestMaxMinVsDoingItYourself test int vs int with static Math classes " + numberOfLoops.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfLoops; x++) { sum += Math.Max(int1 + (x % 5), int2 + (x % 4)); sum -= Math.Min(int1 + (x % 4), int2 + (x % 5)); } sw.Stop(); Console.WriteLine("Sum: " + sum.ToString("#,##0")); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine(); sum = 0; Thread.Sleep(500); Console.WriteLine("###########################################################"); Console.WriteLine("Starting TestMaxMinVsDoingItYourself test int vs int doing it ourselves " + numberOfLoops.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfLoops; x++) { int modResult1 = x % 5; int modResult2 = x % 4; int y = int1 + modResult1; int z = int2 + modResult2; //Max if (y > z) sum += y; else if (y < z) sum += z; //Min y = int1 + modResult2; z = int2 + modResult1; if (y < z) sum -= y; else if (y > z) sum -= z; } sw.Stop(); Console.WriteLine("Sum: " + sum.ToString("#,##0")); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine(); sum = 0; Thread.Sleep(500); Console.WriteLine("###########################################################"); Console.WriteLine("Starting TestMaxMinVsDoingItYourself test double vs double with static Math classes " + numberOfLoops.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfLoops; x++) { sum += Math.Max(doub1 + (x % 5), doub2 + (x % 4)); sum -= Math.Min(doub1 + (x % 4), doub2 + (x % 5)); } sw.Stop(); Console.WriteLine("Sum: " + sum.ToString("#,##0")); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine(); sum = 0; Thread.Sleep(500); Console.WriteLine("###########################################################"); Console.WriteLine("Starting TestMaxMinVsDoingItYourself test double vs double doing it ourselves " + numberOfLoops.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfLoops; x++) { double modResult1 = x % 5; double modResult2 = x % 4; double y = doub1 + modResult1; double z = doub2 + modResult2; //Max if (y > z) sum += y; else if (y < z) sum += z; //Min y = doub1 + modResult2; z = doub2 + modResult1; if (y < z) sum -= y; else if (y > z) sum -= z; } sw.Stop(); Console.WriteLine("Sum: " + sum.ToString("#,##0")); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine(); sum = 0; Thread.Sleep(500); Console.WriteLine("###########################################################"); Console.WriteLine("Starting TestMaxMinVsDoingItYourself test int vs int vs int with static Math classes " + numberOfLoops.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfLoops; x++) { sum += Math.Max(Math.Max(int1 + (x % 5), int2 + (x % 4)), int3 + (x % 3)); sum -= Math.Min(Math.Min(int1 + (x % 3), int2 + (x % 4)), int3 + (x % 5)); } sw.Stop(); Console.WriteLine("Sum: " + sum.ToString("#,##0")); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine(); sum = 0; Thread.Sleep(500); Console.WriteLine("###########################################################"); Console.WriteLine("Starting TestMaxMinVsDoingItYourself test int vs int vs int doing it ourselves " + numberOfLoops.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfLoops; x++) { int modResult1 = x % 5; int modResult2 = x % 4; int modResult3 = x % 3; int y = int1 + modResult1; int z = int2 + modResult2; int w = int3 + modResult3; //Max if (y > z) { if (y > w) sum += y; else if (y < w) sum += w; } else if (y < z) if (z > w) sum += z; else if (z < w) sum += w; //Min y = int1 + modResult3; z = int2 + modResult2; w = int3 + modResult1; if (y < z) { if (y < w) sum -= y; else if (y > w) sum -= w; } else if (y > z) if (z < w) sum -= z; else if (z > w) sum -= w; } sw.Stop(); Console.WriteLine("Sum: " + sum.ToString("#,##0")); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine(); sum = 0; Thread.Sleep(500); Console.WriteLine("###########################################################"); Console.WriteLine("Starting TestMaxMinVsDoingItYourself test double vs double vs double with static Math classes " + numberOfLoops.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfLoops; x++) { sum += Math.Max(Math.Max(doub1 + (x % 5), doub2 + (x % 4)), doub3 + (x % 3)); sum -= Math.Min(Math.Min(doub1 + (x % 3), doub2 + (x % 4)), doub3 + (x % 5)); } sw.Stop(); Console.WriteLine("Sum: " + sum.ToString("#,##0")); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine(); sum = 0; Thread.Sleep(500); Console.WriteLine("###########################################################"); Console.WriteLine("Starting TestMaxMinVsDoingItYourself test double vs double vs double doing it ourselves " + numberOfLoops.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfLoops; x++) { double modResult1 = x % 5; double modResult2 = x % 4; double modResult3 = x % 3; double y = doub1 + modResult1; double z = doub2 + modResult2; double w = doub3 + modResult3; //Max if (y > z) { if (y > w) sum += y; else if (y < w) sum += w; } else if (y < z) if (z > w) sum += z; else if (z < w) sum += w; //Min y = doub1 + modResult3; z = doub2 + modResult2; w = doub3 + modResult1; if (y < z) { if (y < w) sum -= y; else if (y > w) sum -= w; } else if (y > z) if (z < w) sum -= z; else if (z > w) sum -= w; } sw.Stop(); Console.WriteLine("Sum: " + sum.ToString("#,##0")); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Console.WriteLine(); sum = 0; } } //namespace |