Created
September 22, 2021 19:50
-
-
Save Hexer10/44a96b9a7ef6914f1edb20fc50dfb6fd to your computer and use it in GitHub Desktop.
This file contains 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 'package:benchmark_harness/benchmark_harness.dart'; | |
final list = List.generate(100000, (index) => index); | |
class CollectionForBenchmark extends BenchmarkBase { | |
const CollectionForBenchmark() : super('CollectionFor'); | |
static void main() { | |
CollectionForBenchmark().report(); | |
} | |
// The benchmark code. | |
@override | |
void run() { | |
[for (final e in list) e + e]; | |
} | |
} | |
class MapBenchmark extends BenchmarkBase { | |
const MapBenchmark() : super('Map'); | |
static void main() { | |
MapBenchmark().report(); | |
} | |
// The benchmark code. | |
@override | |
void run() { | |
list.map((e) => e + e).toList(); | |
} | |
} | |
void main(List<String> arguments) { | |
// dart <file> | |
// CollectionFor(RunTime): 14212.148936170213 us. | |
// Map(RunTime): 11609.630057803468 us. | |
// ./<file> | |
// CollectionFor(RunTime): 12459.814814814816 us. | |
// Map(RunTime): 15028.320895522387 us. | |
CollectionForBenchmark.main(); | |
MapBenchmark.main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment