Skip to content

Instantly share code, notes, and snippets.

@hrkrx
Last active April 10, 2018 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrkrx/5e44feb7ae15bb0fdd7995b1ba1d4728 to your computer and use it in GitHub Desktop.
Save hrkrx/5e44feb7ae15bb0fdd7995b1ba1d4728 to your computer and use it in GitHub Desktop.
DistanceCalculation preformance
# 1000 points
Generated 1000 samples in 0 ms
Calculated 999000 distances in 19 ms
# 10000 points
Generated 10000 samples in 2 ms
Calculated 99990000 distances in 1974 ms
# 100000 points
Generated 100000 samples in 23 ms
Unhandled Exception: OutOfMemoryException. (Not enough ram)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Security.Cryptography;
namespace CalculationTest
{
class Program
{
static private RNGCryptoServiceProvider __random = new RNGCryptoServiceProvider ();
static void Main(string[] args)
{
var samples = 10000;
Vector3[] points = new Vector3[samples];
var buffer = new byte[4];
Stopwatch sw1 = new Stopwatch();
sw1.Start();
for (int i = 0; i < samples; i++)
{
__random.GetBytes(buffer);
var x = BitConverter.ToInt32(buffer, 0);
__random.GetBytes(buffer);
var y = BitConverter.ToInt32(buffer, 0);
__random.GetBytes(buffer);
var z = BitConverter.ToInt32(buffer, 0);
points[i] = new Vector3(x, y,z);
}
sw1.Stop();
Console.WriteLine("Generated " + samples + " samples in " + sw1.ElapsedMilliseconds + " ms");
List<double> results = new List<double>();
Stopwatch sw2 = new Stopwatch();
sw2.Start();
foreach (var v1 in points)
{
foreach (var v2 in points)
{
if (v1 != v2)
{
results.Add(CalculateDistance(v1, v2));
}
}
}
sw2.Stop();
Console.WriteLine("Calculated " + results.Count + " distances in " + sw2.ElapsedMilliseconds + " ms");
}
static double CalculateDistance(Vector3 v1, Vector3 v2)
{
double result = -1;
result = Math.Sqrt((v2.X - v1.X) + (v2.Y - v1.Y) + (v2.Z - v1.Z));
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment