Skip to content

Instantly share code, notes, and snippets.

@stream-iori
Last active April 26, 2016 03:04
Show Gist options
  • Select an option

  • Save stream-iori/36ba90aa3baaa037b6c457ee55ccafdd to your computer and use it in GitHub Desktop.

Select an option

Save stream-iori/36ba90aa3baaa037b6c457ee55ccafdd to your computer and use it in GitHub Desktop.
skynet Benchmarks test for quasar
import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.strands.channels.Channel;
import co.paralleluniverse.strands.channels.Channels;
/**
* Created by stream.
*/
public class Skynet extends Fiber<Void> {
private Channel<Long> ch;
private long num;
private int size;
private int div;
private Skynet(Channel<Long> ch, long num, int size, int div) {
this.ch = ch;
this.num = num;
this.size = size;
this.div = div;
}
@Override
protected Void run() throws SuspendExecution, InterruptedException {
if (size == 1) {
ch.send(num);
return null;
}
final Channel<Long> rc = Channels.newChannel(0);
long sum = 0;
for (int i = 0; i < div; i++) {
long subSum = num + i * (size / div);
new Skynet(rc, subSum, size / div, div).start();
}
for (int i = 0; i < div; i++) {
sum += rc.receive();
}
ch.send(sum);
return null;
}
public static void main(String[] args) throws InterruptedException, SuspendExecution {
final Channel<Long> ch = Channels.newChannel(0);
long startTime = System.currentTimeMillis();
new Skynet(ch, 0, 1000000, 10).start();
long result = ch.receive();
System.out.format("Result: %s in %d ms. \n", result, (System.currentTimeMillis() - startTime));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment