Created
August 18, 2015 16:42
-
-
Save Scorpiion/7abcb1b7e7d5cda98112 to your computer and use it in GitHub Desktop.
benchmark_mirrors_2.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First results: