forked from skoppe/printFloat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed_rnd.d
98 lines (74 loc) · 2.23 KB
/
speed_rnd.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import std.stdio;
import std.datetime: MonoTime;
import std.random;
import std.math;
import core.memory;
import f1 = format_old;
import f2 = format_new;
void main(string[] args)
{
FloatingPointControl fpctrl;
auto r_tab = [FloatingPointControl.roundDown, FloatingPointControl.roundUp,
FloatingPointControl.roundToZero, FloatingPointControl.roundToNearest];
uint seed = uniform(0,uint.max);
stderr.writeln("seed: ",seed);
auto rnd = Random(seed);
ulong count = 0;
ulong[2] sum = 0;
ulong[256][2] e_sum = 0;
ulong[256] e_count = 0;
ulong faster = 0;
foreach (i;0..100_000_000)
{
if (i%1_000_000==0) { stderr.write("."); stderr.flush(); }
A a;
a.u = uniform!"[]"(0,uint.max,rnd);
int exp = (a.u >> 23) & ((1L << 8) - 1);
import std.conv: to;
string format = "%"
~ (uniform(0,2,rnd)==0?"-":"")
~ (uniform(0,2,rnd)==0?"+":"")
~ (uniform(0,2,rnd)==0?" ":"")
~ (uniform(0,2,rnd)==0?"0":"")
~ (uniform(0,2,rnd)==0?"#":"")
~ (uniform(0,100,rnd)==0?to!string(uniform(0,200,rnd)):"")
~ (uniform(0,100,rnd)==0?"." ~ to!string(uniform(0,200,rnd)):"")
~ "fF"[uniform(0,2,rnd)];
fpctrl.rounding = r_tab[uniform(0,4,rnd)];
string r_old;
string r_new;
ulong d1;
ulong d2;
{
GC.minimize();
auto start = MonoTime.currTime;
r_old = f1.format(format,a.f);
auto delta = (MonoTime.currTime-start).total!"nsecs";
d1 = delta;
}
{
GC.minimize();
auto start = MonoTime.currTime;
r_new = f2.format(format,a.f);
auto delta = (MonoTime.currTime-start).total!"nsecs";
d2 = delta;
}
++count;
sum[0] += d1;
sum[1] += d2;
++e_count[exp];
e_sum[0][exp] += d1;
e_sum[1][exp] += d2;
if (d2<d1) ++faster;
}
stderr.writeln();
foreach (i;0..256)
writeln(e_sum[0][i]/e_count[i], " ", e_sum[1][i]/e_count[i]);
writeln;
writeln(sum[0]/count, " ",sum[1]/count," ",faster," ",count);
}
union A
{
float f;
uint u;
}