Last active
April 26, 2016 03:04
-
-
Save stream-iori/36ba90aa3baaa037b6c457ee55ccafdd to your computer and use it in GitHub Desktop.
skynet Benchmarks test for quasar
This file contains hidden or 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
| 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