root/trunk/benchmark/nsievebits.md

Revision 362, 0.8 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.nsievebits
2
3 // n = 11, 155 sec
4 // laptop, 107.87 sec
5
6 function primes(n)
7 {
8     local count = 0
9     local size = 10000 << n
10
11     local flags = array.new(size / 32 + 1, -1)
12
13     for(prime: 2 .. size + 1)
14     {
15         local offset = prime / 32
16         local mask = 1 << (prime % 32)
17
18         if((flags[offset] & mask) != 0)
19         {
20             count++
21
22             for(i: prime + prime .. size + 1, prime)
23             {
24                 offset = i / 32
25                 mask = 1 << (i % 32)
26
27                 if((flags[offset] & mask) != 0)
28                     flags[offset] = flags[offset] ^ mask
29             }
30         }
31     }
32
33     writefln("Primes up to {,8} {,8}", size, count)
34 }
35
36 function main(N)
37 {
38     local n = 11
39
40     if(isString(N))
41         try n = toInt(N); catch(e) {}
42
43     local timer = time.Timer.clone()
44     timer.start()
45
46         for(i: 0 .. 3)
47             primes(n - i)
48
49     timer.stop()
50     writefln("Took {} sec", timer.seconds())
51 }
Note: See TracBrowser for help on using the browser.