| Replace V with Nim 472eb49 nandithebull 8h ago | 1 | // Headless benchmark: the same Mandelbrot frame rendered by Nim and by Dart. |
| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 10h ago | 2 | // |
| 3 | // LD_LIBRARY_PATH=build dart example/lib/fractal_bench.dart |
| 4 | // |
| 5 | // Also checks the two implementations agree pixel-for-pixel, which is what |
| 6 | // makes the timing comparison meaningful. |
| Replace V with Nim 472eb49 nandithebull 8h ago | 7 | import 'package:nimflutter_ffi/nimflutter_ffi.dart' as v; |
| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 10h ago | 8 | |
| 9 | import 'mandelbrot_dart.dart'; |
| 10 | |
| 11 | Future<void> main() async { |
| 12 | const view = v.FractalView(width: 800, height: 600, maxIter: 500); |
| 13 | final pixels = view.width * view.height; |
| 14 | |
| 15 | // Warm both paths: first call pays for library load / JIT. |
| 16 | v.render(const v.FractalView(width: 64, height: 64, maxIter: 50)); |
| 17 | renderDart(const v.FractalView(width: 64, height: 64, maxIter: 50)); |
| 18 | |
| 19 | final swV = Stopwatch()..start(); |
| Replace V with Nim 472eb49 nandithebull 8h ago | 20 | final fromNim = v.render(view); |
| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 10h ago | 21 | swV.stop(); |
| 22 | |
| 23 | final swP = Stopwatch()..start(); |
| 24 | final fromParallel = await v.renderParallel(view, tiles: 4); |
| 25 | swP.stop(); |
| 26 | |
| 27 | final swD = Stopwatch()..start(); |
| 28 | final fromDart = renderDart(view); |
| 29 | swD.stop(); |
| 30 | |
| 31 | print('${view.width}x${view.height}, maxIter ${view.maxIter} ' |
| 32 | '($pixels px)\n'); |
| Replace V with Nim 472eb49 nandithebull 8h ago | 33 | print('Nim, one isolate ${swV.elapsedMilliseconds} ms'); |
| 34 | print('Nim, 4 isolates ${swP.elapsedMilliseconds} ms'); |
| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 10h ago | 35 | print('Dart, one isolate ${swD.elapsedMilliseconds} ms'); |
| 36 | print('speedup vs Dart ' |
| 37 | '${(swD.elapsedMicroseconds / swV.elapsedMicroseconds).toStringAsFixed(2)}x ' |
| 38 | 'serial, ' |
| 39 | '${(swD.elapsedMicroseconds / swP.elapsedMicroseconds).toStringAsFixed(2)}x ' |
| 40 | 'parallel'); |
| 41 | |
| 42 | print('\nagreement checks'); |
| Replace V with Nim 472eb49 nandithebull 8h ago | 43 | print(' Nim serial vs Nim parallel : ${_diff(fromNim, fromParallel)} bytes differ'); |
| 44 | print(' Nim serial vs Dart : ${_diff(fromNim, fromDart)} bytes differ'); |
| Add a Mandelbrot example app, and a V kernel worth demonstrating 81540bb nandithebull 10h ago | 45 | } |
| 46 | |
| 47 | int _diff(List<int> a, List<int> b) { |
| 48 | if (a.length != b.length) return -1; |
| 49 | var n = 0; |
| 50 | for (var i = 0; i < a.length; i++) { |
| 51 | if (a[i] != b[i]) n++; |
| 52 | } |
| 53 | return n; |
| 54 | } |