Last active
July 26, 2017 16:03
-
-
Save OskarSigvardsson/05be9f302811cf8393ee579c8fdad9aa to your computer and use it in GitHub Desktop.
Sets of three common numbers benchmark
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.Diagnostics; | |
using System.Collections.Generic; | |
public class Tester { | |
public static void Main() { | |
var count = 1000000; | |
var tris0 = new int[count * 3]; | |
var tris1 = new int[count * 3]; | |
var rand = new Random(); | |
var t0 = new int[3]; | |
var t1 = new int[3]; | |
for (int t = 0; t < tris0.Length; t+=3) { | |
t0[0] = (int)(rand.NextDouble() * 50); | |
t0[1] = (int)(rand.NextDouble() * 50); | |
t0[2] = (int)(rand.NextDouble() * 50); | |
t1[0] = (int)(rand.NextDouble() * 50); | |
t1[1] = (int)(rand.NextDouble() * 50); | |
t1[2] = (int)(rand.NextDouble() * 50); | |
tris0[t] = t0[0]; | |
tris0[t+1] = t0[1]; | |
tris0[t+2] = t0[2]; | |
tris1[t] = t1[0]; | |
tris1[t+1] = t1[1]; | |
tris1[t+2] = t1[2]; | |
Array.Sort(t0); | |
Array.Sort(t1); | |
if (t0[0] == t1[0] && t0[1] == t1[1] && t0[2] == t1[2]) { | |
// Triangles are identical, try again. | |
t -= 3; | |
continue; | |
} | |
if (t0[0] == t0[1] || t0[1] == t0[2]) { | |
// Triangle contains repeated value, try again. | |
t -= 3; | |
continue; | |
} | |
if (t1[0] == t1[1] || t1[1] == t1[2]) { | |
// Triangle contains repeated value, try again. | |
t -= 3; | |
continue; | |
} | |
} | |
var watch = new Stopwatch(); | |
watch.Start(); | |
var c1 = SharedEdgeCount1(tris0, tris1); | |
watch.Stop(); | |
var time1 = watch.Elapsed; | |
watch.Restart(); | |
var c2 = SharedEdgeCount2(tris0, tris1); | |
watch.Stop(); | |
var time2 = watch.Elapsed; | |
watch.Restart(); | |
var c3 = SharedEdgeCount3(tris0, tris1); | |
watch.Stop(); | |
var time3 = watch.Elapsed; | |
watch.Restart(); | |
var c4 = SharedEdgeCount4(tris0, tris1); | |
watch.Stop(); | |
var time4 = watch.Elapsed; | |
watch.Restart(); | |
var c5 = SharedEdgeCount5(tris0, tris1); | |
watch.Stop(); | |
var time5 = watch.Elapsed; | |
Console.WriteLine("Original method result: \t" + c1 + ", time elapsed in ms: \t" + time1.TotalMilliseconds); | |
Console.WriteLine("qwertyman method result: \t" + c2 + ", time elapsed in ms: \t" + time2.TotalMilliseconds); | |
Console.WriteLine("midjji method result: \t" + c3 + ", time elapsed in ms: \t" + time3.TotalMilliseconds); | |
Console.WriteLine("Single HashSet method result: \t" + c4 + ", time elapsed in ms: \t" + time4.TotalMilliseconds); | |
Console.WriteLine("Many HashSets method result: \t" + c5 + ", time elapsed in ms: \t" + time5.TotalMilliseconds); | |
} | |
static int SharedEdgeCount1(int[] tris0, int[] tris1) { | |
var count = 0; | |
for (int t = 0; t < tris0.Length; t+=3) { | |
var x0 = tris0[t]; | |
var x1 = tris0[t+1]; | |
var x2 = tris0[t+2]; | |
var y0 = tris1[t]; | |
var y1 = tris1[t+1]; | |
var y2 = tris1[t+2]; | |
if (x0 == y0) { | |
if (x1 == y1 || x1 == y2 || x2 == y1 || x2 == y2) count++; | |
} else if (x0 == y1) { | |
if (x1 == y0 || x1 == y2 || x2 == y0 || x2 == y2) count++; | |
} else if (x0 == y2) { | |
if (x1 == y0 || x1 == y1 || x2 == y0 || x2 == y1) count++; | |
} else { | |
if ((x1 == y0 && x2 == y1) | |
|| (x1 == y0 && x2 == y2) | |
|| (x1 == y1 && x2 == y0) | |
|| (x1 == y1 && x2 == y2) | |
|| (x1 == y2 && x2 == y0) | |
|| (x1 == y2 && x2 == y1)) { | |
count++; | |
} | |
} | |
} | |
return count; | |
} | |
static int SharedEdgeCount2(int[] tris0, int[] tris1) { | |
var count = 0; | |
for (int t = 0; t < tris0.Length; t+=3) { | |
var x0 = tris0[t]; | |
var x1 = tris0[t+1]; | |
var x2 = tris0[t+2]; | |
var y0 = tris1[t]; | |
var y1 = tris1[t+1]; | |
var y2 = tris1[t+2]; | |
int n = 0; | |
// check if x0 is among the values in the second set | |
if (x0 == y0 || x0 == y1 || x0 == y2) { | |
n++; | |
} | |
// check if x1 is among the values in the second set | |
if (x1 == y0 || x1 == y1 || x1 == y2) { | |
n++; | |
} | |
// check if x2 is among the values in the second set | |
if (x2 == y0 || x2 == y1 || x2 == y2) { | |
n++; | |
} | |
if (n >= 2) count++; | |
} | |
return count; | |
} | |
static int SharedEdgeCount3(int[] tris0, int[] tris1) { | |
var count = 0; | |
for (int t = 0; t < tris0.Length; t+=3) { | |
var x0 = tris0[t]; | |
var x1 = tris0[t+1]; | |
var x2 = tris0[t+2]; | |
var y0 = tris1[t]; | |
var y1 = tris1[t+1]; | |
var y2 = tris1[t+2]; | |
bool t0= (x0==y0) ||(x0==y1)|| (x0==y2); | |
bool t1= (x1==y0) ||(x1==y1)|| (x1==y2); | |
bool t2= (x2==y0) ||(x2==y1)|| (x2==y2); | |
if ((t0 && t1) || (t0 && t2) || (t1 && t2)) { | |
count++; | |
} | |
} | |
return count; | |
} | |
static int SharedEdgeCount4(int[] tris0, int[] tris1) { | |
var count = 0; | |
var set = new HashSet<int>(); | |
for (int t = 0; t < tris0.Length; t+=3) { | |
var x0 = tris0[t]; | |
var x1 = tris0[t+1]; | |
var x2 = tris0[t+2]; | |
var y0 = tris1[t]; | |
var y1 = tris1[t+1]; | |
var y2 = tris1[t+2]; | |
set.Clear(); | |
set.Add(y0); | |
set.Add(y1); | |
set.Add(y2); | |
var n = 0; | |
if (set.Contains(x0)) n++; | |
if (set.Contains(x1)) n++; | |
if (set.Contains(x2)) n++; | |
if (n >= 2) count++; | |
} | |
return count; | |
} | |
static int SharedEdgeCount5(int[] tris0, int[] tris1) { | |
var count = 0; | |
for (int t = 0; t < tris0.Length; t+=3) { | |
var set = new HashSet<int>(); | |
var x0 = tris0[t]; | |
var x1 = tris0[t+1]; | |
var x2 = tris0[t+2]; | |
var y0 = tris1[t]; | |
var y1 = tris1[t+1]; | |
var y2 = tris1[t+2]; | |
set.Add(y0); | |
set.Add(y1); | |
set.Add(y2); | |
var n = 0; | |
if (set.Contains(x0)) n++; | |
if (set.Contains(x1)) n++; | |
if (set.Contains(x2)) n++; | |
if (n >= 2) count++; | |
} | |
return count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment