| OLD | NEW |
| (Empty) |
| 1 library loop_perf; | |
| 2 | |
| 3 import 'package:benchmark_harness/benchmark_harness.dart'; | |
| 4 | |
| 5 class IterationBenchmark extends BenchmarkBase { | |
| 6 List<int> list = new List.generate(1000, (i) => i); | |
| 7 Map map; | |
| 8 var r = 0; | |
| 9 IterationBenchmark(name) : super(name) { | |
| 10 map = new Map.fromIterable(list, key: (i) => i, value: (i) => i); | |
| 11 } | |
| 12 } | |
| 13 | |
| 14 class ForEach extends IterationBenchmark { | |
| 15 ForEach() : super('forEach'); | |
| 16 run() { | |
| 17 var count = 0; | |
| 18 list.forEach((int i) => count = count + i); | |
| 19 return count; | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 class ForEachMap extends IterationBenchmark { | |
| 24 ForEachMap() : super('forEachMap'); | |
| 25 run() { | |
| 26 var count = 0; | |
| 27 map.forEach((int k, int v) => count = count + k + v); | |
| 28 return count; | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 class ForIn extends IterationBenchmark { | |
| 33 ForIn() : super('for in'); | |
| 34 run() { | |
| 35 var count = 0; | |
| 36 for(int item in list) { | |
| 37 count = count + item; | |
| 38 } | |
| 39 return count; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 class ForInMap extends IterationBenchmark { | |
| 44 ForInMap() : super('for in Map'); | |
| 45 run() { | |
| 46 var count = 0; | |
| 47 for(int key in map.keys) { | |
| 48 count = count + key + map[key]; | |
| 49 } | |
| 50 return count; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 class ForLoop extends IterationBenchmark { | |
| 55 ForLoop() : super('for loop'); | |
| 56 run() { | |
| 57 int count = 0; | |
| 58 for(int i = 0; i < list.length; i++) { | |
| 59 count += list[i]; | |
| 60 } | |
| 61 return count; | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 void main() { | |
| 66 new ForEach().report(); | |
| 67 new ForIn().report(); | |
| 68 new ForLoop().report(); | |
| 69 new ForEachMap().report(); | |
| 70 new ForInMap().report(); | |
| 71 } | |
| OLD | NEW |