root/trunk/benchmark/chameneos.md

Revision 362, 1.7 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.chameneos
2
3 // n = 1_000_000, 15.816 sec (meh)
4 // laptop: 10.398
5
6 local N = 1_000_000
7
8 local first, second
9 local blue = 1
10 local red = 2
11 local yellow = 3
12 local faded = 4
13
14 function meet(me)
15 {
16     while(second)
17         yield()
18
19     local other = first
20
21     if(other)
22     {
23         first = null
24         second = me
25     }
26     else
27     {
28         local n = N - 1
29
30         if(n < 0)
31             return
32
33         N = n
34         first = me
35
36         do
37         {
38             yield()
39             other = second
40         } while(!other)
41
42         second = null
43
44         yield()
45     }
46
47     return other
48 }
49
50 function creature(color) =
51     coroutine function()
52     {
53         local me = color
54
55         for(met : 0 .. 1_000_000_001)
56         {
57             local other = meet(me)
58
59             if(!other)
60                 return met
61
62             if(me != other)
63             {
64                 if(me == blue)
65                 {
66                     if(other == red)
67                         me = yellow
68                     else
69                         me = red
70                 }
71                 else if(me == red)
72                 {
73                     if(other == blue)
74                         me = yellow
75                     else
76                         me = blue
77                 }
78                 else
79                 {
80                     if(other == blue)
81                         me = red
82                     else
83                         me = blue
84                 }
85             }
86         }
87     }
88
89 function schedule(threads)
90 {
91     local numThreads = #threads
92     local meetings = 0
93
94     while(true)
95     {
96         for(i : 0 .. numThreads)
97         {
98             local thr = threads[i]
99
100             if(!thr)
101                 return meetings
102
103             if(!thr.isDead())
104             {
105                 local met = thr()
106
107                 if(met)
108                 {
109                     meetings += met
110                     threads[i] = null
111                 }
112             }
113         }
114     }
115 }
116
117 function main(n)
118 {
119     if(isString(n))
120         try N = toInt(n); catch(e) {}
121
122     local timer = time.Timer.clone()
123     timer.start()
124
125     local threads =
126     [
127         creature(blue),
128         creature(red),
129         creature(yellow),
130         creature(blue)
131     ]
132
133     writefln((schedule(threads)))
134
135     timer.stop()
136     writefln("Took {} sec", timer.seconds())
137 }
Note: See TracBrowser for help on using the browser.