public static void ConvexHull() { int[] testSizes = new int[] { 12345, 100, 316, 1000, 3160, 10000, 31600, 100000, 316000, 1000000, 3160000, 10000000 }; for (int iter = 0; iter < testSizes.Length; iter++) { Random r = new Random(); List <PointD> points = new List <PointD>(testSizes[iter]); for (int i = 0; i < points.Capacity; i++) { double size = r.NextDouble(); double ang = r.NextDouble() * (Math.PI * 2); points.Add(new PointD(size * Math.Cos(ang), size * Math.Sin(ang))); } // Plus: test sorting time to learn how much of the time is spent sorting var points2 = new List <PointD>(points); EzStopwatch timer = new EzStopwatch(true); points2.Sort((a, b) => a.X == b.X ? a.Y.CompareTo(b.Y) : (a.X < b.X ? -1 : 1)); Stopwatch timer2 = new Stopwatch(); timer2.Start(); int sortTime = timer.Restart(); IListSource <Point <double> > output = PointMath.ComputeConvexHull(points, true); int hullTime = timer.Millisec; Console.WriteLine("{0:c} (ticks:{1,10} freq:{2})", timer2.Elapsed, timer2.ElapsedTicks, Stopwatch.Frequency); if (iter == 0) { continue; // first iteration primes the JIT/caches } Console.WriteLine("Convex hull of {0,8} points took {1} ms ({2} ms for sorting step). Output has {3} points.", testSizes[iter], hullTime, sortTime, output.Count); } }
/// <summary>Runs a piece of code one or more times and records the time taken.</summary> /// <remarks>Garbage-collects before the test(s) if DoGC is true.</remarks> public void RunCore(int minMillisec, Func <Benchmarker, object> code, int loopTimes, bool noGC, EzStopwatch totalTime) { // Give the test as good a chance as possible to avoid garbage collection if (DoGC && !noGC) { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } var oldTimer = _currentTimer; try { _currentTimer = new EzStopwatch(true); do { _currentTimer.Restart(); object userData = null; for (int i = 0; i < loopTimes; i++) { userData = code(this); } double millisec = _currentTimer.Millisec; if (userData as string != DiscardResult) { Tally(ActiveBenchmarkName, millisec, userData); } } while (totalTime.Millisec < minMillisec); } finally { _currentTimer = oldTimer; } }