Last active
May 29, 2016 05:25
-
-
Save mattak/f834d54f5dd102fdcfd152ca19bf44df to your computer and use it in GitHub Desktop.
Utility function for calculating performance.
This file contains hidden or 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 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"); | |
} | |
} | |
} | |
} |
This file contains hidden or 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.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