Created
January 18, 2014 19:40
-
-
Save aarondandy/8495172 to your computer and use it in GitHub Desktop.
Simple point performance test.
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.Diagnostics; | |
using System.IO; | |
namespace PointBandwidthTest | |
{ | |
class Program | |
{ | |
/* | |
* Best in seconds | |
* Metric | Point | Double | |
* Allocation ~610M| .5079264 | .4503237 | |
* Distance Sum | .4685041 | .4617855 | |
* Write Text ~1.4G| 34.1495408 | 33.4280593 | |
* Write Bin ~610M | 4.5442429 | 4.3699137 | |
*/ | |
private static readonly Random RandomGen = new Random((int)DateTime.Now.Ticks); | |
private const double ValueRange = 10; | |
private const int TestPointCount = 40000000; | |
private static readonly double[] RandomNumbers; | |
private static int RandomIndex; | |
static Program() { | |
RandomNumbers = new double[TestPointCount*2]; | |
for (int i = 0; i < RandomNumbers.Length; i++) | |
RandomNumbers[i] = RandomGen.NextDouble()*ValueRange; | |
ResetNumberIndex(); | |
} | |
static double GetANumber() { return RandomNumbers[++RandomIndex]; } | |
static void ResetNumberIndex() { RandomIndex = -1; } | |
public struct Point | |
{ | |
public readonly double X; | |
public readonly double Y; | |
public Point(double x, double y) { | |
X = x; | |
Y = y; | |
} | |
public double MagnitudeSquared { get { return (X*X) + (Y*Y); } } | |
public double Magnitude { get { return Math.Sqrt(MagnitudeSquared); } } | |
} | |
static void Main(string[] args) { | |
for (int i = 0; i < 3; i++) { | |
ResetNumberIndex(); | |
var points = CreateAndProcessPointsArray(); | |
WriteText(points); | |
WriteBin(points); | |
} | |
Console.WriteLine("-----------------------"); | |
for (int i = 0; i < 3; i++) { | |
ResetNumberIndex(); | |
var doubles = CreateAndProcessDoubleArray(); | |
WriteText(doubles); | |
WriteBin(doubles); | |
} | |
Console.WriteLine("-----------------------"); | |
} | |
private static double[] CreateAndProcessDoubleArray() { | |
var clock = new Stopwatch(); | |
clock.Start(); | |
var doubles = new double[TestPointCount * 2]; | |
for (int i = 0; i < doubles.Length; i++) { | |
doubles[i] = GetANumber(); | |
} | |
clock.Stop(); | |
Console.WriteLine("Allocated doubles:\t" + clock.Elapsed); | |
clock.Restart(); | |
double magnitudeSum = 0; | |
for (int i = 0; i < doubles.Length;) { | |
var x = doubles[i++]; | |
var y = doubles[i++]; | |
magnitudeSum += Math.Sqrt((x * x)+(y * y)); | |
} | |
clock.Stop(); | |
Console.WriteLine("Process doubles:\t" + clock.Elapsed); | |
Console.WriteLine("Sum: " + magnitudeSum); | |
return doubles; | |
} | |
private static void WriteText(double[] doubles) { | |
var stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
using (var file = File.Open("doubles.txt", FileMode.Create)) | |
using (var writer = new StreamWriter(file)) { | |
for (int i = 0; i < doubles.Length; ) { | |
writer.Write(doubles[i++]); | |
writer.Write(' '); | |
writer.WriteLine(doubles[i++]); | |
} | |
} | |
stopwatch.Stop(); | |
Console.WriteLine("WriteText doubles:\t" + stopwatch.Elapsed); | |
} | |
private static void WriteBin(double[] doubles) { | |
var stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
using (var file = File.Open("doubles.bin", FileMode.Create)) | |
using (var writer = new BinaryWriter(file)) { | |
for (int i = 0; i < doubles.Length; i++) | |
writer.Write(doubles[i]); | |
} | |
stopwatch.Stop(); | |
Console.WriteLine("WriteBin doubles:\t" + stopwatch.Elapsed); | |
} | |
private static Point[] CreateAndProcessPointsArray() { | |
var clock = new Stopwatch(); | |
clock.Start(); | |
var points = new Point[TestPointCount]; | |
for (int i = 0; i < points.Length; ++i) { | |
points[i] = new Point(GetANumber(), GetANumber()); | |
} | |
clock.Stop(); | |
Console.WriteLine("Allocated points:\t" + clock.Elapsed); | |
clock.Restart(); | |
double magnitudeSum = 0; | |
for (int i = 0; i < points.Length; ++i) { | |
magnitudeSum += points[i].Magnitude; | |
} | |
clock.Stop(); | |
Console.WriteLine("Process points:\t\t" + clock.Elapsed); | |
Console.WriteLine("Sum: " + magnitudeSum); | |
return points; | |
} | |
private static void WriteText(IEnumerable<Point> points) { | |
var stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
using (var file = File.Open("points.txt", FileMode.Create)) | |
using (var writer = new StreamWriter(file)) { | |
foreach(var point in points) { | |
writer.Write(point.X); | |
writer.Write(' '); | |
writer.WriteLine(point.Y); | |
} | |
} | |
stopwatch.Stop(); | |
Console.WriteLine("WriteText points:\t" + stopwatch.Elapsed); | |
} | |
private static void WriteBin(Point[] points) { | |
var stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
using (var file = File.Open("points.bin", FileMode.Create)) | |
using (var writer = new BinaryWriter(file)) { | |
foreach (var point in points) { | |
writer.Write(point.X); | |
writer.Write(point.Y); | |
} | |
} | |
stopwatch.Stop(); | |
Console.WriteLine("WriteBin points:\t" + stopwatch.Elapsed); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment