Making Samples executable. #!/usr/bin/env io and chmod a+x *.io
[io/jrb1.git] / samples / shootout / cheapconcurrency.io
blob7e9ddb0a624523e5215462560808d5f18bdb6c0f
1 #!/usr/bin/env io
3 /* The Computer Language Shootout
4 http://shootout.alioth.debian.org
5 Contributed by Gavin Harrison */
7 times := args at(1) asNumber
9 Coroutine setStackSize(30000)
11 sum := 0
13 Thread := Object clone do(
14 foo := method(n,
15 if(next, next @@foo(n + 1), Lobby sum = sum + n + 1)
19 last := nil
21 500 repeat(
22 t := Thread clone
23 t next := last
24 last = t
27 first := t
28 args at(1) asNumber repeat(first @@foo(0))
30 while(Coroutine yieldingCoros size > 0, yield)
32 writeln(sum)
35 times := args at(1) asNumber
37 sum := 0
39 Thread := Coroutine clone do(
40 init := method(
41 self coro := Coroutine clone setStackSize(15000)
42 coro setRunTarget(self)
43 coro setRunLocals(self)
44 coro setRunMessage(message(process))
45 coro run
48 receive := method(n,
49 self n := n
50 coro resume
53 process := method(
54 mainCoro resume
55 loop(
56 if(next, next n := n + 1; next coro resume, Lobby sum = sum + n + 1; mainCoro resume)
61 last := nil
63 mainCoro := Coroutine currentCoroutine
65 500 repeat(
66 t := Thread clone
67 t next := last
68 last = t
71 //Collector setDebug(true)
73 first := t
74 args at(1) asNumber repeat(first receive(0))
75 writeln(sum)
80 times := args at(1) asNumber
81 sum := 0
82 n := 0
84 send := method(
85 if(n > 1,
86 n = n - 1
87 coro := Coroutine clone setStackSize(20000)
88 coro setRunTarget(Lobby)
89 coro setRunLocals(Lobby)
90 coro setRunMessage(message(send))
91 coro run
92 , Lobby sum = sum + 500; mainCoro resume)
94 mainCoro := Coroutine currentCoroutine
96 iters := args at(1) asNumber
97 for(i, 1, iters,
98 n = 500
99 send
100 //writeln("iter ", i, "\t", sum, "\t", (Collector maxAllocatedBytes/1000000) asString(0,2))
101 //Collector collect
103 writeln(sum)