| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library dart.pkg.isolate.sample.runners; | |
| 6 | |
| 7 import "package:isolate/loadbalancer.dart"; | |
| 8 import "package:isolate/isolaterunner.dart"; | |
| 9 import "dart:async" show Future, Completer; | |
| 10 | |
| 11 void main() { | |
| 12 int N = 44; | |
| 13 var sw = new Stopwatch()..start(); | |
| 14 // Compute fib up to 42 with 4 isolates. | |
| 15 parfib(N, 4).then((v1) { | |
| 16 var t1 = sw.elapsedMilliseconds; | |
| 17 sw.stop(); | |
| 18 sw.reset(); | |
| 19 print("fib#4(${N}) = ${v1[N]}, ms: $t1"); | |
| 20 sw.start(); | |
| 21 // Then compute fib up to 42 with 2 isolates. | |
| 22 parfib(N, 2).then((v2) { | |
| 23 var t2 = sw.elapsedMilliseconds; | |
| 24 sw.stop(); | |
| 25 print("fib#2(${N}) = ${v2[N]}, ms: $t2"); | |
| 26 }); | |
| 27 }); | |
| 28 } | |
| 29 | |
| 30 // Compute fibonnacci 1..limit | |
| 31 Future<List<int>> parfib(int limit, int parallelity) { | |
| 32 return LoadBalancer.create(parallelity, IsolateRunner.spawn).then( | |
| 33 (LoadBalancer pool) { | |
| 34 List<Future> fibs = new List(limit + 1); | |
| 35 // Schedule all calls with exact load value and the heaviest task | |
| 36 // assigned first. | |
| 37 schedule(a, b, i) { | |
| 38 if (i < limit) { | |
| 39 schedule(a + b, a, i + 1); | |
| 40 } | |
| 41 fibs[i] = pool.run(fib, i, load: a); | |
| 42 } | |
| 43 schedule(0, 1, 0); | |
| 44 // And wait for them all to complete. | |
| 45 return Future.wait(fibs).whenComplete(pool.close); | |
| 46 }); | |
| 47 } | |
| 48 | |
| 49 int computeFib(n) { | |
| 50 int result = fib(n); | |
| 51 return result; | |
| 52 } | |
| 53 | |
| 54 int fib(n) { | |
| 55 if (n < 2) return n; | |
| 56 return fib(n - 1) + fib(n - 2); | |
| 57 } | |
| OLD | NEW |