Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Threading;
- namespace ProfileMathMax
- {
- class Program
- {
- static double controlTotalSeconds;
- const uint InnerLoopCount = 100000;
- const uint OuterLoopCount = 4000000000 / InnerLoopCount; // increased by x4
- static int[] values = new int[InnerLoopCount];
- static int total = 0;
- static void ProfileBase()
- {
- var stopwatch = new Stopwatch();
- stopwatch.Start();
- int maxValue;
- for (int j = 0; j < OuterLoopCount; j++)
- {
- maxValue = 0;
- for (int i = 0; i < InnerLoopCount; i++)
- {
- // baseline
- total += values[i];
- }
- }
- stopwatch.Stop();
- controlTotalSeconds = stopwatch.Elapsed.TotalSeconds;
- Console.WriteLine("Control - Empty Loop - " + controlTotalSeconds + " seconds");
- }
- static double ProfileMathMax()
- {
- int maxValue;
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- for (int j = 0; j < OuterLoopCount; j++)
- {
- maxValue = 0;
- for (int i = 0; i < InnerLoopCount; i++)
- {
- maxValue = Math.Max(values[i], maxValue);
- total += values[i];
- }
- }
- stopwatch.Stop();
- Console.WriteLine("Math.Max(a, max) - " + stopwatch.Elapsed.TotalSeconds + " seconds");
- Console.WriteLine("Relative: " + (stopwatch.Elapsed.TotalSeconds - controlTotalSeconds) + " seconds");
- return stopwatch.Elapsed.TotalSeconds;
- }
- static double ProfileMathMaxReverse()
- {
- int maxValue;
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- for (int j = 0; j < OuterLoopCount; j++)
- {
- maxValue = 0;
- for (int i = 0; i < InnerLoopCount; i++)
- {
- maxValue = Math.Max(maxValue, values[i]);
- total += values[i];
- }
- }
- stopwatch.Stop();
- Console.WriteLine("Math.Max(max, a) - " + stopwatch.Elapsed.TotalSeconds + " seconds");
- Console.WriteLine("Relative: " + (stopwatch.Elapsed.TotalSeconds - controlTotalSeconds) + " seconds");
- return stopwatch.Elapsed.TotalSeconds;
- }
- static double ProfileInline()
- {
- int maxValue = 0;
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- for (int j = 0; j < OuterLoopCount; j++)
- {
- maxValue = 0;
- for (int i = 0; i < InnerLoopCount; i++)
- {
- maxValue = maxValue > values[i] ? values[i] : maxValue;
- total += values[i];
- }
- }
- stopwatch.Stop();
- Console.WriteLine("max = max > a ? a : max: " + stopwatch.Elapsed.TotalSeconds + " seconds");
- Console.WriteLine("Relative: " + (stopwatch.Elapsed.TotalSeconds - controlTotalSeconds) + " seconds");
- return stopwatch.Elapsed.TotalSeconds;
- }
- static double ProfileIf()
- {
- int maxValue = 0;
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- for (int j = 0; j < OuterLoopCount; j++)
- {
- maxValue = 0;
- for (int i = 0; i < InnerLoopCount; i++)
- {
- if (values[i] > maxValue)
- maxValue = values[i];
- total += values[i];
- }
- }
- stopwatch.Stop();
- Console.WriteLine("if (a > max) max = a: " + stopwatch.Elapsed.TotalSeconds + " seconds");
- Console.WriteLine("Relative: " + (stopwatch.Elapsed.TotalSeconds - controlTotalSeconds) + " seconds");
- return stopwatch.Elapsed.TotalSeconds;
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Version 2.1");
- // wait one second to prime it - make sure everything .NET needs to run is loaded
- Thread.Sleep(1000);
- Random rnd = new Random();
- // run twice again to prime it - make sure any optimizations the processor does for code is done
- ProfileBase();
- Thread.Sleep(1000);
- ProfileBase();
- Console.WriteLine();
- Console.WriteLine("Running Worse Case...");
- for (int i = 0; i < InnerLoopCount; i++)
- {
- values[i] = i; // worst case: every new number biggest than the previous
- //values[i] = i == 0 ? 1 : 0; // best case: first number is the maximum
- //values[i] = rnd.Next(int.MaxValue); // average case: random numbers
- }
- var worseCase = PerformTests();
- Console.WriteLine("Running Best Case");
- for (int i = 0; i < InnerLoopCount; i++)
- {
- values[i] = i == 0 ? 1 : 0; // best case: first number is the maximum
- }
- var bestCase = PerformTests();
- Console.WriteLine("Running Average...");
- for (int i = 0; i < InnerLoopCount; i++)
- {
- values[i] = rnd.Next(int.MaxValue); // average case: random numbers
- }
- var average = PerformTests();
- //var relativeTo = controlTotalSeconds;
- var relativeTo = Min(
- worseCase.mathMax,
- bestCase.mathMax,
- average.mathMax,
- worseCase.mathReverse,
- bestCase.mathReverse,
- average.mathReverse,
- worseCase.inline,
- bestCase.inline,
- average.inline,
- worseCase.@if,
- bestCase.@if,
- average.@if
- );
- WriteResults(relativeTo, worseCase, bestCase, average);
- Console.ReadLine();
- }
- private static void WriteResults(double relativeTo, PerformTestResult worseCase, PerformTestResult bestCase, PerformTestResult average)
- {
- Console.WriteLine("max = Math.Max(max, a): {0:0.000} sec best case / {1:0.000} sec worst case / {2:0.000} sec average case", bestCase.mathMax - relativeTo, worseCase.mathMax - relativeTo, average.mathMax - relativeTo);
- Console.WriteLine("max = Math.Max(a, max): {0:0.000} sec best case / {1:0.000} sec worst case / {2:0.000} sec average case", bestCase.mathReverse - relativeTo, worseCase.mathReverse - relativeTo, average.mathReverse - relativeTo);
- Console.WriteLine("max = max > a ? max : a: {0:0.000} sec best case / {1:0.000} sec worst case / {2:0.000} sec average case", bestCase.inline - relativeTo, worseCase.inline - relativeTo, average.inline - relativeTo);
- Console.WriteLine("if (a > max) max = a: {0:0.000} sec best case / {1:0.000} sec worst case / {2:0.000} sec average case", bestCase.@if - relativeTo, worseCase.@if - relativeTo, average.@if - relativeTo);
- }
- private static double Min(params double[] values) {
- var min = double.MaxValue;
- for(var i = 0; i < values.Length; i++) {
- min = Math.Min(min, values[i]);
- }
- return min;
- }
- private struct PerformTestResult
- {
- public double mathMax { get; set; }
- public double mathReverse { get; set; }
- public double inline { get; set; }
- public double @if { get; set; }
- }
- private static PerformTestResult PerformTests()
- {
- // run twice again to prime it - make sure any optimizations the processor does for code is done
- ProfileMathMax();
- var mathMax = ProfileMathMax();
- Console.WriteLine();
- ProfileMathMaxReverse();
- var mathReverse = ProfileMathMaxReverse();
- Console.WriteLine();
- ProfileInline();
- var inline = ProfileInline();
- Console.WriteLine();
- ProfileIf();
- var @if = ProfileIf();
- Console.WriteLine();
- return new PerformTestResult
- {
- mathMax = mathMax,
- mathReverse = mathReverse,
- inline = inline,
- @if = @if
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement