2021-08-09 15:02:28 +00:00
|
|
|
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:isolate';
|
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:expect/expect.dart';
|
|
|
|
|
|
|
|
// Implements recursive summation via tail calls:
|
|
|
|
// fib(n) => n <= 1 ? 1
|
|
|
|
// : fib(n-1) + fib(n-2);
|
|
|
|
Future fibonacciRecursive(List args) async {
|
|
|
|
final SendPort port = args[0];
|
|
|
|
final n = args[1];
|
|
|
|
if (n <= 1) {
|
|
|
|
port.send(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final left = ReceivePort();
|
|
|
|
final right = ReceivePort();
|
|
|
|
await Future.wait([
|
|
|
|
Isolate.spawn(fibonacciRecursive, [left.sendPort, n - 1]),
|
|
|
|
Isolate.spawn(fibonacciRecursive, [right.sendPort, n - 2]),
|
|
|
|
]);
|
|
|
|
final results = await Future.wait([left.first, right.first]);
|
|
|
|
port.send(results[0] + results[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> main() async {
|
|
|
|
final rpWarmup = ReceivePort();
|
|
|
|
final rpRun = ReceivePort();
|
|
|
|
final int nWarmup = 17; // enough runs to trigger optimized compilation
|
|
|
|
final int nWarmupFactorial = 2584;
|
2021-11-04 18:57:21 +00:00
|
|
|
// Runs for about 8 seconds.
|
2021-08-09 15:02:28 +00:00
|
|
|
final int n = 21;
|
|
|
|
final int nFactorial = 17711;
|
|
|
|
final beforeRss = ProcessInfo.currentRss;
|
|
|
|
|
|
|
|
int maxRss = beforeRss;
|
|
|
|
final rssTimer = Timer.periodic(const Duration(milliseconds: 10), (_) {
|
|
|
|
maxRss = max(ProcessInfo.currentRss, maxRss);
|
|
|
|
});
|
|
|
|
|
|
|
|
final watch = Stopwatch();
|
|
|
|
watch.start();
|
|
|
|
|
2021-11-04 18:57:21 +00:00
|
|
|
// Warm up code by running a couple iterations in the main isolate.
|
2021-08-09 15:02:28 +00:00
|
|
|
await Isolate.spawn(fibonacciRecursive, [rpWarmup.sendPort, nWarmup]);
|
|
|
|
Expect.equals(nWarmupFactorial, await rpWarmup.first);
|
|
|
|
|
|
|
|
final warmup = watch.elapsedMicroseconds;
|
|
|
|
|
|
|
|
await Isolate.spawn(fibonacciRecursive, [rpRun.sendPort, n]);
|
|
|
|
Expect.equals(nFactorial, await rpRun.first);
|
|
|
|
|
|
|
|
final done = watch.elapsedMicroseconds;
|
|
|
|
|
2021-11-04 18:57:21 +00:00
|
|
|
print('IsolateFibonacci_$n.Calculation(RunTimeRaw): ${done - warmup} us.');
|
|
|
|
print('IsolateFibonacci_$n.DeltaPeak(MemoryUse): ${maxRss - beforeRss}');
|
2021-08-09 15:02:28 +00:00
|
|
|
rssTimer.cancel();
|
|
|
|
}
|