Skip to content

Instantly share code, notes, and snippets.

@jadudm
Created August 10, 2012 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadudm/3315898 to your computer and use it in GitHub Desktop.
Save jadudm/3315898 to your computer and use it in GitHub Desktop.
"Chinese Whispers" in occam-pi
-- 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!)
:
@jadudm
Copy link
Author

jadudm commented Aug 10, 2012

A version without embedded timing code is here:

https://gist.github.com/3315932

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment