Skip to content

Instantly share code, notes, and snippets.

@Scorpiion
Created August 18, 2015 16:42
Show Gist options
  • Save Scorpiion/7abcb1b7e7d5cda98112 to your computer and use it in GitHub Desktop.
Save Scorpiion/7abcb1b7e7d5cda98112 to your computer and use it in GitHub Desktop.
benchmark_mirrors_2.dart
import 'dart:async';
import 'dart:mirrors';
class Class {
int field;
}
final mirror = reflectClass(Class);
final constructor = const Symbol('');
main() async {
await testIterations(100, 'a hundred');
await testIterations(1000, 'a thousand');
await testIterations(10000, '10 thousand');
await testIterations(20000, '20 thousand');
await testIterations(50000, '50 thousand');
await testIterations(100000, 'a hundred thousand');
await testIterations(1000000, 'a million');
await testIterations(2000000, 'two million');
await testIterations(5000000, 'five million');
await testIterations(10000000, 'ten million');
}
testIterations(int times, String description) async {
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
await 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;
await 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) async {
for (var i in new List<int>.filled(times, 1)) {
await testWitMirrorsAction(i);
}
}
testWithoutMirrors(int times) async {
for (var i in new List<int>.filled(times, 1)) {
await 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

First results:

a hundred instantiations and assignments:
  using mirrors: 1.88 ms
  without using mirrors: 0.5 ms
  Mirrors was 3.76 times slower.
a thousand instantiations and assignments:
  using mirrors: 3.3 ms
  without using mirrors: 3.04 ms
  Mirrors was 1.0855263157894737 times slower.
10 thousand instantiations and assignments:
  using mirrors: 40.24 ms
  without using mirrors: 41.16 ms
  Mirrors was 0.9776482021379982 times slower.
20 thousand instantiations and assignments:
  using mirrors: 127.98 ms
  without using mirrors: 169.18 ms
  Mirrors was 0.7564723962643338 times slower.

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