-
Notifications
You must be signed in to change notification settings - Fork 1
/
test1.d
54 lines (45 loc) · 1.17 KB
/
test1.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
/+
usage:
ldc2 -release -O2 -run dtools/scratch/test1.d
expected output:
reltime=20.7386(%); time(base)=2333 time(t2)=2817
reltime=20.8055(%); time(base)=2334 time(t2)=2820
-1670913744
+/
module dtools.scratch.test1;
import std.conv;
import std.datetime;
import std.stdio;
void dispBenchmark(T)(T t1, T t2){
auto t=t1.length.to!real;
auto r=(t2.length.to!real-t)/t;
writefln(`reltime=%s(%%); time(base)=%s time(t2)=%s`,r*100, t1.to!("msecs",int), t2.to!("msecs",int));
}
int funb(int x, int i){
return (x+1)*(x-1);
}
ref int foo(bool checkbounds)(ref int x, int*ptr){
foreach(i;0..8)
x+=funb(x-i,i*x)+funb(x+i,i*x);
static if(checkbounds){ // bounds checking (attempt for having safe references)
if(&x>ptr){
assert(0);
}
}
return x;
}
static int x=0;
void fun(bool checkbounds)(){
int _temp;
x+=foo!checkbounds(x,&_temp);
}
void test(){
enum n=100_000_000;
auto b = comparingBenchmark!(fun!false, fun!true,n);
dispBenchmark(b.baseTime, b.targetTime);
// doing the same in reversed order just to make sure there's no disadvantage of being first.
b = comparingBenchmark!(fun!true, fun!false,n);
dispBenchmark(b.targetTime, b.baseTime);
writeln(x);
}
void main(){test;}