Skip to content

Instantly share code, notes, and snippets.

@mattak
Last active May 29, 2016 05:25
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 mattak/f834d54f5dd102fdcfd152ca19bf44df to your computer and use it in GitHub Desktop.
Save mattak/f834d54f5dd102fdcfd152ca19bf44df to your computer and use it in GitHub Desktop.
Utility function for calculating performance.
using System;
using NUnit.Framework;
namespace GeoHex
{
[TestFixture]
public class GeoHexPerformanceTest
{
[Test]
public void Pow3Test()
{
const int MaxRepeat = 1000000;
const int MaxLevel = 19;
{
TimeWatch.Reset();
TimeWatch.Resume();
for (int repeat = 0; repeat < MaxRepeat; repeat++)
{
for (int i = 0; i < MaxLevel; i++)
{
Pow3.Calc(i);
}
}
TimeWatch.Pause(MaxLevel*MaxRepeat);
TimeWatch.OutputResult("Pow3");
}
{
TimeWatch.Reset();
TimeWatch.Resume();
for (int repeat = 0; repeat < MaxRepeat; repeat++)
{
for (int i = 0; i < MaxLevel; i++)
{
Math.Pow(3,i);
}
}
TimeWatch.Pause(MaxLevel*MaxRepeat);
TimeWatch.OutputResult("Math.Pow3");
}
}
}
}
using System;
using System.IO;
public static class TimeWatch
{
private static long _currentTime = 0;
private static long _count = 0;
private static long _totalTime = 0;
public static void Reset()
{
_currentTime = 0;
_totalTime = 0;
_count = 0;
}
public static void Resume()
{
_currentTime = CurrentTimeMillis();
}
public static long Pause(int countup = 1)
{
long diff = CurrentTimeMillis() - _currentTime;
_count += countup;
_totalTime += diff;
return diff;
}
public static void OutputResult(string tag)
{
PrintResult(tag);
WriteResult(tag);
}
public static void PrintResult(string tag)
{
Console.WriteLine($"{tag}\t{_count}\t{_totalTime}\t{(double) _totalTime/_count}");
}
public static void WriteResult(string tag)
{
using (var sw = new StreamWriter(
"/tmp/geohex_test.tsv",
true,
System.Text.Encoding.GetEncoding("utf-8")
))
{
sw.WriteLine($"{tag}\t{_count}\t{_totalTime}\t{(double) _totalTime/_count}");
sw.Close();
}
}
private static long CurrentTimeMillis()
{
System.DateTime epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
return (long) (System.DateTime.UtcNow - epochStart).TotalMilliseconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment