dart-sdk/benchmarks/LongStringCompare/dart/LongStringCompare.dart
Josh Soref 4d7839dfda Spelling benchmarks
Closes https://github.com/dart-lang/sdk/pull/50787

GitOrigin-RevId: 7cf1129d9bee2a4c1a857767dadae11d81e927f1
Change-Id: Ie7028f94966755d565843b8f6e2590f2243ff937
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276685
Reviewed-by: Jonas Termansen <sortie@google.com>
Commit-Queue: Jonas Termansen <sortie@google.com>
2022-12-20 12:36:37 +00:00

51 lines
1.3 KiB
Dart

// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// Measure performance of string comparison.
import 'package:benchmark_harness/benchmark_harness.dart';
int equalCount = 0;
class LongStringCompare extends BenchmarkBase {
final int reps;
final List<String> s = [];
String generateLongString(int lengthPower) {
return "abc" * (1 << lengthPower) + "def";
}
LongStringCompare(int lengthPower, this.reps)
: super('LongStringCompare.${1 << lengthPower}.${reps}reps') {
final single = generateLongString(lengthPower);
s.add(single + "." + single);
s.add(single + "!" + single);
}
@override
void warmup() {
for (int i = 0; i < reps / 2; i++) {
run();
}
}
@override
void run() {
for (int i = 0; i < reps; i++) {
// Make string comparison code hoisting harder for the compiler to do.
bool comparison = s[i % 2] == s[(i + 1) % 2];
if (comparison) {
equalCount++;
}
}
}
}
void main() {
LongStringCompare(1, 3000).report();
LongStringCompare(5, 1000).report();
LongStringCompare(10, 30).report();
if (equalCount > 0) throw StateError("Unexpected equalCount: $equalCount");
}