Skip to content

Instantly share code, notes, and snippets.

@josephbales
Created January 25, 2020 05:10
Show Gist options
  • Save josephbales/19c431c1698214618992fd6148e27b19 to your computer and use it in GitHub Desktop.
Save josephbales/19c431c1698214618992fd6148e27b19 to your computer and use it in GitHub Desktop.
Test proc speed
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp8
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Starting Non-Parallel Test\n");
List<double> nonParallelTimes = new List<double>();
for (int i = 1; i < 6; i++)
{
nonParallelTimes.Add(DoNonParallelTest(i));
}
Console.WriteLine($"The average non-parallel run time was: {nonParallelTimes.Average()}");
Console.WriteLine("Starting Parallel Test\n");
List<double> parallelTimes = new List<double>();
for (int i = 1; i < 6; i++)
{
parallelTimes.Add(DoParallelTest(i));
}
Console.WriteLine($"The average parallel run time was: {parallelTimes.Average()}");
}
private static double DoNonParallelTest(int run)
{
Console.WriteLine($"Starting Run #{run}");
long fromInc = 0;
long toEx = 300000000;
Stopwatch stopWatch = Stopwatch.StartNew();
for (long i = fromInc; i < toEx; i++)
{
if (Math.Abs(i % 3.1415927) < 0.0001) { }
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
double time = Convert.ToDouble($"{ts.Seconds}.{ts.Milliseconds}");
Console.WriteLine($"Ending Run #{run} with elapsed time of {time}");
return time;
}
private static double DoParallelTest(int run)
{
Console.WriteLine($"Starting Run #{run}");
long fromInc = 0;
long toEx = 2000000000;
Stopwatch stopWatch = Stopwatch.StartNew();
Parallel.For(fromInc, toEx, (i) =>
{
if (Math.Abs(i % 3.1415927) < 0.0001) { }
});
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
double time = Convert.ToDouble($"{ts.Seconds}.{ts.Milliseconds}");
Console.WriteLine($"Ending Run #{run} with elapsed time of {time}");
return time;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment