dart-sdk/benchmarks/LongStringCompare/dart/LongStringCompare.dart
William Hesse 80a16a16cb [dart] Move LongStringCompare benchmark to correct directory
Bug: https://github.com/dart-lang/sdk/issues/50190
Change-Id: If9d350622217100a6882c10978e4434d51f4fba3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/271081
Commit-Queue: Jonas Termansen <sortie@google.com>
Reviewed-by: Jonas Termansen <sortie@google.com>
Auto-Submit: William Hesse <whesse@google.com>
2022-11-21 12:03:33 +00:00

50 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 hoisiting 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");
}