root/trunk/benchmark/partialsums.md

Revision 362, 1.4 kB (checked in by JarrettBillingsley, 3 months ago)

Fixed a bug where array data was being allocated but not initialized, causing some GC cycles to try to scan garbage data (and crash). Updated the benchmarks and all that I've tested have worked fine, not to mention being at least 15% faster in most cases :)

Line 
1 module benchmark.partialsums
2
3 // n = 2,500,000, 21.74 sec
4 // on laptop, 13.996 sec
5
6 function main(N)
7 {
8     local n = 2_500_000
9
10     if(isString(N))
11         try n = toInt(N); catch(e) {}
12
13     local timer = time.Timer.clone()
14     timer.start()
15
16         local a1 = 1.0
17         local a2 = 0.0
18         local a3 = 0.0
19         local a4 = 0.0
20         local a5 = 0.0
21         local a6 = 0.0
22         local a7 = 0.0
23         local a8 = 0.0
24         local a9 = 0.0
25         local alt = 1.0
26         local sqrt = math.sqrt
27         local sin = math.sin
28         local cos = math.cos
29         local pow = math.pow
30
31         for(local k = 1.0; k <= n; k += 1.0)
32         {
33             local k2 = k * k
34             local sk = sin(k)
35             local ck = cos(k)
36             local k3 = k2 * k
37
38             a1 += pow(2.0 / 3.0, k)
39             a2 += 1.0 / sqrt(k)
40             a3 += 1.0 / (k2 + k)
41
42             // Flint Hills
43             a4 += 1.0 / (k3 * sk * sk)
44
45             // Cookson Hills
46             a5 += 1.0 / (k3 * ck * ck)
47
48             // Harmonic
49             a6 += 1.0 / k
50
51             // Riemann zeta
52             a7 += 1.0 / k2
53
54             // Alternating harmonic
55             a8 += alt / k
56
57             // Gregory
58             a9 += alt / (k + k - 1)
59
60             alt = -alt
61         }
62
63         writefln("{:9}\t(2/3)^k", a1)
64         writefln("{:9}\tk^-0.5", a2)
65         writefln("{:9}\t1/k(k+1)", a3)
66         writefln("{:9}\tFlint Hills", a4)
67         writefln("{:9}\tCookson Hills", a5)
68         writefln("{:9}\tHarmonic", a6)
69         writefln("{:9}\tRiemann Zeta", a7)
70         writefln("{:9}\tAlternating Harmonic", a8)
71         writefln("{:9}\tGregory", a9)
72
73     timer.stop()
74     writefln("Took {} secs", timer.seconds())
75 }
Note: See TracBrowser for help on using the browser.