|
| 1 | +import openlocationcode; |
| 2 | + |
| 3 | +import core.time : MonoTime; |
| 4 | +import std.random; |
| 5 | +import std.stdio; |
| 6 | + |
| 7 | +void main() |
| 8 | +{ |
| 9 | + enum loops = 1000000; |
| 10 | + |
| 11 | + struct TestData |
| 12 | + { |
| 13 | + double lat, lon; |
| 14 | + int length; |
| 15 | + string code; |
| 16 | + |
| 17 | + static TestData generate() |
| 18 | + { |
| 19 | + TestData ret; |
| 20 | + ret.lat = uniform01!double * 180 - 90; |
| 21 | + ret.lon = uniform01!double * 360 - 180; |
| 22 | + ret.length = uniform(4, 15); |
| 23 | + if (ret.length < 10 && ret.length % 2 == 1) |
| 24 | + ret.length++; |
| 25 | + ret.code = OpenLocationCode.encode(ret.lat, ret.lon, ret.length).code; |
| 26 | + return ret; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + TestData[] list; |
| 31 | + list.length = loops; |
| 32 | + foreach (i; 0 .. loops) |
| 33 | + list[i] = TestData.generate(); |
| 34 | + |
| 35 | + writeln("Running encode benchmark"); |
| 36 | + ubyte[maxOLCLength] buffer = void; |
| 37 | + // benchmark encode |
| 38 | + auto start = MonoTime.currTime(); |
| 39 | + foreach (data; list) |
| 40 | + cast(void) OpenLocationCode.encode(buffer[], data.lat, data.lon, data.length); |
| 41 | + auto end = MonoTime.currTime(); |
| 42 | + auto dur = end - start; |
| 43 | + writefln("Encode %s loops in %s;\n\t%s per call", loops, dur, dur / loops); |
| 44 | + |
| 45 | + writeln("Running decode benchmark"); |
| 46 | + // benchmark decode |
| 47 | + start = MonoTime.currTime(); |
| 48 | + foreach (data; list) |
| 49 | + cast(void) OpenLocationCode.fromTrustedString(data.code).decodeTrustedFull(); |
| 50 | + end = MonoTime.currTime(); |
| 51 | + dur = end - start; |
| 52 | + writefln("Decode %s loops in %s;\n\t%s per call", loops, dur, dur / loops); |
| 53 | +} |
0 commit comments