1 /+ dub.sdl:
2     dependency "numparse" path=".."
3  +/
4 module bench.floating;
5 
6 import std.datetime.stopwatch;
7 import std : uniform, tuple, Tuple, format, to, stderr, abs;
8 
9 import numparse;
10 
11 enum N = 5_000_000;
12 
13 float test()
14 {
15     alias T = float;
16     Tuple!(T, string)[] list;
17     list.length = N;
18 
19     immutable fmt = "%.6f";
20 
21     foreach (i; 0 .. N)
22     {
23         const v = uniform(-1000.0f, 1000.0f);
24         const str = format(fmt, v);
25         list[i] = tuple(str.to!T, str);
26     }
27 
28     static void checkValid(T orig, T parsed, string str, size_t line=__LINE__)
29     {
30         if (abs(orig - parsed) <= 1e-6) return;
31         throw new Exception("wrong parse: %s(%f) parsed as %f".format(str, orig, parsed), __FILE__, line);
32     }
33 
34     auto t0 = StopWatch(AutoStart.yes);
35     foreach (v; list)
36     {
37         T tmp;
38         auto err = parseSimpleFloatNumber!10(tmp, v[1]);
39         if (err != ParseError.none)
40             throw new Exception("parse error: %s (%s)".format(err, v[1]));
41         checkValid(v[0], tmp, v[1]);
42     }
43     t0.stop();
44 
45     auto t1 = StopWatch(AutoStart.yes);
46     foreach (v; list)
47     {
48         T tmp;
49         if (v[1].length) tmp = v[1].to!T;
50         checkValid(v[0], tmp, v[1]);
51     }
52     t1.stop();
53 
54     const t0f = cast(float)t0.peek().total!"hnsecs";
55 
56     stderr.writeln("numparse: ", t0.peek());
57     stderr.writeln("std impl: ", t1.peek());
58     const win = t1.peek().total!"hnsecs" / t0f;
59     stderr.writeln("     win: ", win);
60     return win;
61 }
62 
63 void main()
64 {
65     stderr.writeln("number count: ", N);
66     stderr.writeln("avg win: ", test());
67 }