Skip to content

Instantly share code, notes, and snippets.

@Scorpiion
Forked from emilniklas/benchmark_mirrors.dart
Created August 18, 2015 15:56
Show Gist options
  • Select an option

  • Save Scorpiion/16ef5c122fd8c24ea7ea to your computer and use it in GitHub Desktop.

Select an option

Save Scorpiion/16ef5c122fd8c24ea7ea to your computer and use it in GitHub Desktop.
dart:mirrors benchmark
import 'dart:async';
import 'dart:mirrors';
class Class {
int field;
}
final mirror = reflectClass(Class);
final constructor = const Symbol('');
main() {
testIterations(100, 'a hundred');
testIterations(1000, 'a thousand');
testIterations(10000, '10 thousand');
testIterations(20000, '20 thousand');
testIterations(50000, '50 thousand');
testIterations(100000, 'a hundred thousand');
testIterations(1000000, 'a million');
testIterations(2000000, 'two million');
testIterations(5000000, 'five million');
testIterations(10000000, 'ten million');
}
testIterations(int times, String description) {
final repeats = 50;
// Repeat 50 times
var durationsMirrors = 0;
for (var i in new List.filled(repeats, 1)) {
// Start times
var starttimeMirrors = new DateTime.now().millisecondsSinceEpoch;
// Run benchmark
testWithMirrors(times);
// Sum up the milliseconds each iteration of the test takes
durationsMirrors += new DateTime.now().millisecondsSinceEpoch - starttimeMirrors;
}
// Get the average milliseconds it took the complete the test
var durationMirrors = durationsMirrors / repeats;
// Do the same for the test without mirrors
var durationsWithoutMirrors = 0;
for (var i in new List.filled(repeats, 1)) {
var starttimeWithoutMirrors = new DateTime.now().millisecondsSinceEpoch;
testWithoutMirrors(times);
durationsWithoutMirrors += new DateTime.now().millisecondsSinceEpoch - starttimeWithoutMirrors;
}
var durationWithoutMirrors = durationsWithoutMirrors / repeats;
// Get how many times slower the mirrors implementation was
var slower = durationMirrors / durationWithoutMirrors;
print('''$description instantiations and assignments:
using mirrors: $durationMirrors ms
without using mirrors: $durationWithoutMirrors ms
${slower.toString() == 'Infinity'
? 'Without using mirrors was to quick to measure'
: 'Mirrors was $slower times slower.'}''');
}
testWithMirrors(int times) {
for (var i in new List<int>.filled(times, 1)) {
testWitMirrorsAction(i);
}
}
testWithoutMirrors(int times) {
for (var i in new List<int>.filled(times, 1)) {
testWithoutMirrorsAction(i);
}
}
Future testWitMirrorsAction(int i) async {
mirror.newInstance(constructor, []).setField(#field, i);
await new Timer(new Duration(microseconds: 1), () => 0);
return new Future.value(i);
}
Future testWithoutMirrorsAction(int i) async {
new Class().field = i;
await new Timer(new Duration(microseconds: 1), () => 0);
return new Future.value(i);
}
@Scorpiion
Copy link
Author

Some of the results:

a hundred instantiations and assignments:
  using mirrors: 0.14 ms
  without using mirrors: 0.12 ms
  Mirrors was 1.1666666666666667 times slower.
a thousand instantiations and assignments:
  using mirrors: 0.3 ms
  without using mirrors: 0.14 ms
  Mirrors was 2.142857142857143 times slower.
10 thousand instantiations and assignments:
  using mirrors: 7.4 ms
  without using mirrors: 13.7 ms
  Mirrors was 0.5401459854014599 times slower.
20 thousand instantiations and assignments:
  using mirrors: 33.06 ms
  without using mirrors: 63.08 ms
  Mirrors was 0.5240963855421688 times slower.
50 thousand instantiations and assignments:
  using mirrors: 205.2 ms
  without using mirrors: 343.54 ms
  Mirrors was 0.5973103568725621 times slower.
a hundred thousand instantiations and assignments:
  using mirrors: 996.08 ms
  without using mirrors: 1453.68 ms
  Mirrors was 0.6852127015574266 times slower.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment