Skip to content

Instantly share code, notes, and snippets.

@banks
Created November 16, 2015 15:44
Show Gist options
  • Save banks/ed49cfeb176842302441 to your computer and use it in GitHub Desktop.
Save banks/ed49cfeb176842302441 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include "../libmill.h"
void relay(chan in, chan out) {
int val = chr(in, int);
chs(out, int, val + 1);
}
static coroutine void worker(chan in, chan out) {
while (1) {
relay(in, out);
}
}
int main(int argc, char *argv[]) {
if(argc != 3) {
printf("usage: whispers <n> <m>\n");
return 1;
}
long n = atol(argv[1]);
long m = atol(argv[2]);
chan leftmost = chmake(int, 0);
chan left = leftmost, right = leftmost;
long i;
for (i = 0; i < (n - 1); ++i) {
right = chmake(int, 0);
go(worker(left, right));
left = right;
}
int64_t start = now();
// Start things off
chs(leftmost, int, 0);
for (i = 0; i < (m - 1); ++i) {
relay(right, leftmost);
}
int res = chr(right, int) + 1;
int64_t stop = now();
long duration = (long)(stop - start);
printf("Got %d \ntook %f seconds\n", res, (float)duration / 1000);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment