Created
August 10, 2012 17:23
-
-
Save jadudm/3315898 to your computer and use it in GitHub Desktop.
"Chinese Whispers" in occam-pi
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
-- As described in this blog post: | |
-- http://cxwangyi.wordpress.com/2012/07/29/chinese-whispers-in-racket-and-go/ | |
-- | |
-- This version was run and tested on the Transterpreter Virtual Machine, a | |
-- bytecode interpreter for occam-pi programs. Compiled natively, it will | |
-- be faster; with modification, it could be run on an Arduino (and be slower). | |
-- | |
-- For more info, see: | |
-- http://concurrency.cc/ | |
#INCLUDE "useful.module" | |
VAL INT N IS 100000: | |
PROC whisper (CHAN INT left?, right!) | |
INT v: | |
SEQ | |
left ? v | |
right ! (v + 1) | |
: | |
PROC main (CHAN BYTE kyb?, scr!, err!) | |
INT s.time, e.time: | |
TIMER t: | |
SEQ | |
t ? s.time | |
[N+1]CHAN INT whispers: | |
PAR | |
-- The first whisperer | |
whispers[0] ! 1 | |
-- N whisperers | |
PAR i = 0 FOR N | |
whisper (whispers[i]?, whispers[i+1]!) | |
-- The last whisperer | |
INT end: | |
SEQ | |
whispers[N] ? end | |
-- Stop timing after comms are done. | |
-- Don't measure the time taken to print. | |
t ? e.time | |
-- Print the end result. | |
out.int(end, 0, scr!) | |
out.string("*n", 0, scr!) | |
-- Show the time it took to run N whisperers | |
-- in microseconds. Timing resolution is different | |
-- if executed on the Arduino. | |
out.int((e.time - s.time) / 1000, 0, scr!) | |
out.string(" us*n", 0, scr!) | |
: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A version without embedded timing code is here:
https://gist.github.com/3315932