-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
This issue was originally filed by [email protected]
The following code times two operations:
- call a closure that calls a method 'tearOff' on a particular object
- call a closure that returns a closure representing the object's 'tearOff' method, then call that closure
On my machine (Mac Pro, OS X 10.5.8) the second approach is about 1700 times slower.
class Test {
Test();
void tearOff() {}
}
class Benchmark {
Test test;
Function closure1;
Function closure2;
Benchmark();
void run() {
test = new Test();
closure1 = () => test.tearOff();
closure2 = () => test.tearOff;
for (int iter = 0; iter < 10; iter++) {
bench();
}
}
void bench() {
Stopwatch w1 = new Stopwatch();
w1.start();
for (int i = 0; i < 10000; i++) {
closure1();
}
w1.stop;
double t1 = w1.elapsedInUs() / 10000;
Stopwatch w2 = new Stopwatch();
w2.start();
for (int i = 0; i < 10000; i++) {
closure2()();
}
w2.stop;
double t2 = w2.elapsedInUs() / 10000;
print("$t1
}
}
void main() {
new Benchmark().run();
}