Skip to content

Instantly share code, notes, and snippets.

@Reprevise
Created February 4, 2024 18:01
Show Gist options
  • Save Reprevise/19f960373162df201930794aa732006a to your computer and use it in GitHub Desktop.
Save Reprevise/19f960373162df201930794aa732006a to your computer and use it in GitHub Desktop.
Various string benchmarks in Dart
import 'dart:math';
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:characters/characters.dart';
const String _kCharset =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
final Random _random = Random();
String _generateRandomString(int length) {
return String.fromCharCodes(
List.generate(
length,
growable: false,
(_) => _kCharset.codeUnitAt(_random.nextInt(_kCharset.length)),
),
);
}
final _randomString = _generateRandomString(256);
final class StringForBenchmark extends BenchmarkBase {
StringForBenchmark() : super('StringForBenchmark');
List<String> _chars = [];
@override
void warmup() {
for (var i = 0; i < 50; i++) {
_chars = [];
run();
}
}
@override
void run() {
for (int i = 0; i < _randomString.length; i++) {
_chars.add(_randomString[i]);
}
}
// @override
// void teardown() {
// print(_chars.length);
// super.teardown();
// }
@override
void exercise() {
for (var i = 0; i < 1000; i++) {
_chars = [];
run();
}
}
}
final class StringSplitBenchmark extends BenchmarkBase {
StringSplitBenchmark() : super('StringSplitBenchmark');
List<String> _chars = [];
@override
void warmup() {
for (var i = 0; i < 50; i++) {
_chars = [];
run();
}
}
@override
void run() {
for (final c in _randomString.split('')) {
_chars.add(c);
}
}
// @override
// void teardown() {
// print(_chars.length);
// super.teardown();
// }
@override
void exercise() {
for (var i = 0; i < 1000; i++) {
_chars = [];
run();
}
}
}
final class CharactersBenchmark extends BenchmarkBase {
CharactersBenchmark() : super('CharactersBenchmark');
List<String> _chars = [];
@override
void warmup() {
for (var i = 0; i < 50; i++) {
_chars = [];
run();
}
}
@override
void run() {
for (final c in _randomString.characters) {
_chars.add(c);
}
}
// @override
// void teardown() {
// print(_chars.length);
// super.teardown();
// }
@override
void exercise() {
for (var i = 0; i < 1000; i++) {
_chars = [];
run();
}
}
}
final class CodeUnitsBenchmark extends BenchmarkBase {
CodeUnitsBenchmark() : super('CodeUnitsBenchmark');
List<String> _chars = [];
@override
void warmup() {
for (var i = 0; i < 50; i++) {
_chars = [];
run();
}
}
@override
void run() {
for (final cu in _randomString.codeUnits) {
_chars.add(String.fromCharCode(cu));
}
}
// @override
// void teardown() {
// print(_chars.length);
// super.teardown();
// }
@override
void exercise() {
for (var i = 0; i < 1000; i++) {
_chars = [];
run();
}
}
}
final class SyncStarBenchmark extends BenchmarkBase {
SyncStarBenchmark() : super('SyncStarBenchmark');
List<String> _chars = [];
@override
void warmup() {
for (var i = 0; i < 50; i++) {
_chars = [];
run();
}
}
@override
void run() {
for (final c in _randomString.chars) {
_chars.add(c);
}
}
// @override
// void teardown() {
// print(_chars.length);
// super.teardown();
// }
@override
void exercise() {
for (var i = 0; i < 1000; i++) {
_chars = [];
run();
}
}
}
final class CustomIterableBenchmark extends BenchmarkBase {
CustomIterableBenchmark() : super('CustomIterableBenchmark');
List<String> _chars = [];
@override
void warmup() {
for (var i = 0; i < 50; i++) {
_chars = [];
run();
}
}
@override
void run() {
for (final c in _randomString.asIterable) {
_chars.add(c);
}
}
// @override
// void teardown() {
// print(_chars.length);
// super.teardown();
// }
@override
void exercise() {
for (var i = 0; i < 1000; i++) {
_chars = [];
run();
}
}
}
extension on String {
Iterable<String> get chars sync* {
for (var i = 0; i < length; i++) {
yield this[i];
}
}
Iterable<String> get asIterable => CustomStringIterable(this);
}
final class CustomStringIterable extends Iterable<String> {
const CustomStringIterable(this._string);
final String _string;
@override
int get length => _string.length;
@override
String elementAt(int index) => _string[index];
@override
Iterator<String> get iterator => CustomStringIterator(_string);
}
final class CustomStringIterator implements Iterator<String> {
CustomStringIterator(this._str);
final String _str;
int _idx = -1;
@override
String get current => _str[_idx];
@override
bool moveNext() => ++_idx < _str.length;
}
void main() {
StringForBenchmark().report();
StringSplitBenchmark().report();
CharactersBenchmark().report();
CodeUnitsBenchmark().report();
SyncStarBenchmark().report();
CustomIterableBenchmark().report();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment