Created
December 14, 2014 01:51
-
-
Save mxrss/eb4b1113ebd01ca11c92 to your computer and use it in GitHub Desktop.
parallel bench mark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Collections.Concurrent; | |
using System.Diagnostics; | |
using System.IO; | |
namespace GameOfSums | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int numberOfIterations = 1000; | |
IList<double> runsNonParallel = new List<double>(); | |
IList<double> runsParallel = new List<double>(); | |
for (int interation = 0; interation != numberOfIterations; interation++) | |
{ | |
using (var watch = new CountDownClock()) | |
{ | |
foreach (var file in Directory.GetFiles(@"C:\Windows\System32")) | |
{ | |
GetFileSize(file); | |
} | |
var elapsed = watch.GetElapsed(); | |
runsNonParallel.Add(elapsed.TotalMilliseconds); | |
} | |
using (var watch = new CountDownClock()) | |
{ | |
IList<string> files = Directory.GetFiles(@"C:\Windows\System32"); | |
Parallel.ForEach(files, (workItem) => | |
{ | |
var x = new FileInfo(workItem).Length; | |
}); | |
var elapsed = watch.GetElapsed(); | |
runsParallel.Add(elapsed.TotalMilliseconds); | |
} | |
} | |
Console.WriteLine("Statistics"); | |
Console.WriteLine("{0} Total Runs", numberOfIterations); | |
Console.WriteLine("{0} Min Non-Parallel [Fastest Run]", runsNonParallel.Min()); | |
Console.WriteLine("{0} Max Non-Parallel [Slowest Run]", runsNonParallel.Max()); | |
Console.WriteLine("{0} Avg Run", runsNonParallel.Average()); | |
Console.WriteLine("{0} Min Parallel [Fastest Run]", runsParallel.Min()); | |
Console.WriteLine("{0} Max Parallel [Slowest Run]", runsParallel.Max()); | |
Console.WriteLine("{0} Avg Run", runsParallel.Average()); | |
Console.ReadLine(); | |
} | |
static long GetFileSize(string fileName) | |
{ | |
return new FileInfo(fileName).Length; | |
} | |
} | |
public class CountDownClock : IDisposable | |
{ | |
Stopwatch watch = new Stopwatch(); | |
public CountDownClock() | |
{ | |
watch.Start(); | |
} | |
public TimeSpan GetElapsed() | |
{ | |
return watch.Elapsed.Duration(); | |
} | |
public void Dispose() | |
{ | |
watch.Stop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment