Last active
April 10, 2018 12:15
-
-
Save hrkrx/5e44feb7ae15bb0fdd7995b1ba1d4728 to your computer and use it in GitHub Desktop.
DistanceCalculation preformance
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
# 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) |
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.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