|
| 1 | +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:benchmark_harness/benchmark_harness.dart'; |
| 6 | +import 'package:collection/collection.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + for (var unordered in [true, false]) { |
| 10 | + DeepCollectionEqualityEqualsBenchmark(unordered).report(); |
| 11 | + DeepCollectionEqualityHashBenchmark(unordered).report(); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class DeepCollectionEqualityBase extends BenchmarkBase { |
| 16 | + final DeepCollectionEquality equality; |
| 17 | + |
| 18 | + DeepCollectionEqualityBase(bool unordered, String function) |
| 19 | + : equality = unordered |
| 20 | + ? const DeepCollectionEquality.unordered() |
| 21 | + : const DeepCollectionEquality(), |
| 22 | + super('DeepCollectionQuality${unordered ? 'Unordered' : ''}.$function'); |
| 23 | +} |
| 24 | + |
| 25 | +class DeepCollectionEqualityHashBenchmark extends DeepCollectionEqualityBase { |
| 26 | + DeepCollectionEqualityHashBenchmark(bool unordered) |
| 27 | + : super(unordered, 'hash'); |
| 28 | + |
| 29 | + @override |
| 30 | + void run() { |
| 31 | + hash = equality.hash(mapA); |
| 32 | + } |
| 33 | + |
| 34 | + static int hash = 0; |
| 35 | +} |
| 36 | + |
| 37 | +class DeepCollectionEqualityEqualsBenchmark extends DeepCollectionEqualityBase { |
| 38 | + DeepCollectionEqualityEqualsBenchmark(bool unordered) |
| 39 | + : super(unordered, 'equals'); |
| 40 | + |
| 41 | + @override |
| 42 | + void run() { |
| 43 | + equals = equality.equals(mapA, mapB); |
| 44 | + } |
| 45 | + |
| 46 | + static bool equals = false; |
| 47 | +} |
| 48 | + |
| 49 | +final mapA = { |
| 50 | + for (var i = 0; i < 100; i++) |
| 51 | + { |
| 52 | + [ |
| 53 | + for (var j = i; j < i + 10; j++) j, |
| 54 | + ]: i.isEven ? i : '$i', |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +final mapB = { |
| 59 | + for (var i = 0; i < 100; i++) |
| 60 | + { |
| 61 | + [ |
| 62 | + for (var j = i; j < i + 10; j++) j, |
| 63 | + ]: i.isEven ? i : '$i', |
| 64 | + } |
| 65 | +}; |
0 commit comments