Program.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using System.Diagnostics;
  8. namespace ABTest
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var random = new Random();
  15. var randomPrisms = 10000;
  16. var iterations = 100000;
  17. var repeat = 10;
  18. var cubeSize = 128;
  19. var octtree = new DwarfCorp.OctTreeNode<int>(new Vector3(0, 0, 0), new Vector3(cubeSize, cubeSize, cubeSize));
  20. for (int i = 0; i < randomPrisms; ++i)
  21. {
  22. var min = new Vector3(random.Next(cubeSize), random.Next(cubeSize), random.Next(cubeSize));
  23. octtree.AddItem(i, new BoundingBox(min, min + Vector3.One));
  24. }
  25. var randomVolumes = new List<BoundingBox>();
  26. for (var i = 0; i < iterations; ++i)
  27. {
  28. var min = new Vector3(random.Next(cubeSize), random.Next(cubeSize), random.Next(cubeSize));
  29. randomVolumes.Add(new BoundingBox(min, min + new Vector3(random.Next(3, 20), random.Next(3, 20), random.Next(3, 20))));
  30. }
  31. var stopwatch = new Stopwatch();
  32. var enumeratorTotal = 0l;
  33. var hashTotal = 0l;
  34. var total = 0l;
  35. for (var i = 0; i < repeat; ++i)
  36. {
  37. stopwatch.Start();
  38. var totalHit = 0;
  39. foreach (var volume in randomVolumes)
  40. totalHit += octtree.EnumerateItems(volume).Count();
  41. stopwatch.Stop();
  42. enumeratorTotal += stopwatch.ElapsedTicks;
  43. Console.WriteLine("HIT: " + totalHit + " - Old speed " + stopwatch.ElapsedTicks);
  44. stopwatch.Reset();
  45. stopwatch.Start();
  46. totalHit = 0;
  47. foreach (var volume in randomVolumes)
  48. {
  49. var hash = new HashSet<int>();
  50. octtree.EnumerateItems(volume, hash);
  51. foreach (var x in hash)
  52. total += x;
  53. totalHit += hash.Count;
  54. }
  55. stopwatch.Stop();
  56. hashTotal += stopwatch.ElapsedTicks;
  57. Console.WriteLine("HIT: " + totalHit + " - New speed " + stopwatch.ElapsedTicks);
  58. }
  59. Console.WriteLine("ENUM: " + enumeratorTotal);
  60. Console.WriteLine("HASH: " + hashTotal);
  61. {
  62. var totalHit = 0;
  63. foreach (var volume in randomVolumes)
  64. totalHit += octtree.EnumerateItems(volume).Distinct().Count();
  65. Console.WriteLine("HIT: " + totalHit);
  66. }
  67. Console.WriteLine(total);
  68. Console.WriteLine((float)hashTotal / (float)enumeratorTotal);
  69. Console.ReadLine();
  70. }
  71. }
  72. }