root/trunk/benchmark/pidigits.md

Revision 362, 1.2 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.pidigits
2
3 // Broken, need a bigInt representation
4
5 function Next(z) =
6     (3 * z[0] + z[1]) / (3 * z[2] + z[3])
7
8 function Safe(z, n) =
9     n == ((4 * z[0] + z[1]) / (4 * z[2] + z[3]))
10
11 function Comp(a, b) =
12     [
13         a[0] * b[0] + a[1] * b[2],
14         a[0] * b[1] + a[1] * b[3],
15         a[2] * b[0] + a[3] * b[2],
16         a[2] * b[1] + a[3] * b[3]
17     ]
18
19 function Prod(z, n) =
20     Comp([10, -10 * n, 0, 1], z)
21
22 function Cons(z, k) =
23     Comp(z, [k, 4 * k + 2, 0, 2 * k + 1])
24
25 function Digit(k, z, n, Row, Col)
26 {
27     if(n > 0)
28     {
29         local y = Next(z)
30
31         if(Safe(z, y))
32         {
33             if(Col == 10)
34             {
35                 writef("\t:{}\n{}", 10 + Row, y)
36                 return Digit(k, Prod(z, y), n - 1, 10 + Row, 1)
37             }
38             else
39             {
40                 write(y)
41                 return Digit(k, Prod(z, y), n - 1, Row, Col + 1)
42             }
43         }
44         else
45             return Digit(k + 1, Cons(z, k), n, Row, Col)
46     }
47     else
48     {
49         write(" ".repeat(10 - Col))
50         writefln("\t:{}", Row + Col)
51     }
52 }
53
54 function Digits(n) =
55     Digit(1, [1, 0, 0, 1], n, 0, 0)
56
57 function main(N)
58 {
59     local n = 2500
60    
61     if(isString(N))
62         try n = toInt(N); catch(e){}
63
64     local timer = time.Timer.clone()
65     timer.start()
66
67         Digits(n)
68
69     timer.stop()
70     writefln("Took {} sec", timer.seconds())
71 }
Note: See TracBrowser for help on using the browser.