Last active
October 17, 2016 03:06
-
-
Save 5alamander/0d9ea1271cedb2b9248658cdcbbff806 to your computer and use it in GitHub Desktop.
chinese whispers game, daisy-chain in go, coffee/js-csp, erlang/elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
csp.go -> | |
console.time 'daisy chain' | |
n = 100000 | |
leftmost = csp.chan() | |
right = leftmost | |
left = leftmost | |
for i in [0...n] | |
right = csp.chan() | |
csp.go (left, right) -> | |
temp = 1 + (yield csp.take right) | |
yield csp.put left, temp | |
, [left, right] | |
left = right | |
# csp.go -> | |
# yield csp.put right, 1 | |
csp.putAsync right, 1 | |
console.timeEnd 'daisy chain' | |
console.log yield csp.take leftmost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private IEnumerator daisyChainFunctor () { | |
var startTime = Time.time; | |
var n = 1000; | |
var leftmost = csp.chan(); | |
var right = leftmost; | |
var left = leftmost; | |
for (var i = 0; i < n; i ++) { | |
right = csp.chan(); | |
// csp.go(this, chain(left, right)); // 11.77s +- 0.5s | |
csp.go(chain(left, right)); // 0.12s +- 0.05s | |
left = right; | |
} | |
csp.putAsync(this, right, 1, _ => { | |
Debug.Log("put succeed"); | |
}); | |
csp.Result ret; | |
yield return csp.take(out ret, leftmost); | |
var endTime = Time.time; | |
Debug.Log("daisy-chain: " + (int) ret.value + " time: " + (endTime - startTime)); | |
} | |
private IEnumerator chain (csp.Channel left, csp.Channel right) { | |
csp.Result ret; | |
yield return csp.take(out ret, right); | |
yield return csp.put(left, 1 + (int) ret.value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"testing" | |
) | |
func Test_DaisyChain(t *testing.T) { | |
n := 100000 | |
leftmost := make(chan int) | |
left := leftmost | |
right := leftmost | |
fmt.Println("start daisy chain") | |
for i := 0; i < n; i++ { | |
right = make(chan int) | |
go func(left chan int, right chan int) { | |
left <- 1 + <-right | |
}(left, right) | |
left = right | |
} | |
right <- 0 | |
fmt.Println(<-leftmost) | |
fmt.Println("end daisy chain") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule DaisyChain do | |
def chain(left) do | |
receive do | |
x -> send left, x + 1 | |
end | |
end | |
def spawnChain(maxN, currentN, left) do | |
pid = spawn DaisyChain, :chain, [left] | |
if currentN < maxN do | |
spawnChain maxN, currentN + 1, pid | |
else | |
pid | |
end | |
end | |
def startLeftmost() do | |
{_, sec, micro} = :erlang.timestamp | |
right = spawnChain 100000, 0, self() | |
send right, 0 | |
receive do | |
x -> | |
{_, sec1, micro1} = :erlang.timestamp | |
deltatime = sec1 + micro1/1000000 - sec - micro/1000000 | |
IO.puts "done #{x}, in #{deltatime}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment