From bc17724feb7f5fbd3fddf02b4c00b79cc626a355 Mon Sep 17 00:00:00 2001 From: Alexander Aprelev Date: Fri, 11 Nov 2022 23:22:58 +0000 Subject: [PATCH] [benchmarks] Add LongStringCompare benchmark that measure string comparison performance. BUG=https://github.com/dart-lang/sdk/issues/50190 TEST=ci Change-Id: I1cb93455283b19cf1a712132920b7d3e1dabcd8a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/269380 Commit-Queue: Alexander Aprelev Reviewed-by: Siva Annamalai --- .../StringCompare/dart/LongStringCompare.dart | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 benchmarks/StringCompare/dart/LongStringCompare.dart diff --git a/benchmarks/StringCompare/dart/LongStringCompare.dart b/benchmarks/StringCompare/dart/LongStringCompare.dart new file mode 100644 index 00000000000..32ea4b3bd2b --- /dev/null +++ b/benchmarks/StringCompare/dart/LongStringCompare.dart @@ -0,0 +1,46 @@ +// 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'; + +class LongStringCompare extends BenchmarkBase { + late String s, t; + + String generateLongString() { + var s = "abcdefgh"; + for (int i = 0; i < 20; i++) { + s = "$s ghijkl $s"; + } + return s; + } + + LongStringCompare() : super('LongStringCompare') { + s = generateLongString(); + t = s; + // Difference in two strings goes in the middle. + s += "." + s; + t += "!" + t; + } + + @override + void warmup() { + for (int i = 0; i < 4; i++) { + run(); + } + } + + @override + void run() { + bool b = true; + for (int i = 0; i < 5; i++) { + b &= (s == t); + } + } +} + +void main() { + LongStringCompare().report(); +}