-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_builder_benchmark.dart
62 lines (51 loc) · 1.28 KB
/
string_builder_benchmark.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import "string_builder.dart";
import 'package:benchmark_harness/benchmark_harness.dart';
int NUM_ITEMS = 10000;
class StringBenchmark extends BenchmarkBase {
const StringBenchmark() : super("String");
static void main() {
new StringBenchmark().report();
}
// The benchmark code.
void run() {
String s = "";
for (int i = 0; i < NUM_ITEMS; i++) {
s += new String.fromCharCode(i);
}
s.toString();
}
}
class StringBufferBenchmark extends BenchmarkBase {
const StringBufferBenchmark() : super("StringBuffer");
static void main() {
new StringBufferBenchmark().report();
}
// The benchmark code.
void run() {
StringBuffer s = new StringBuffer();
for (int i = 0; i < NUM_ITEMS; i++) {
s.writeCharCode(i);
}
s.toString();
}
}
class StringBuilderBenchmark extends BenchmarkBase {
const StringBuilderBenchmark() : super("StringBuilder");
static void main() {
new StringBuilderBenchmark().report();
}
// The benchmark code.
void run() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < NUM_ITEMS; i++) {
sb.appendChar(i);
}
sb.toString();
}
}
// Main function runs the benchmark.
main() {
StringBenchmark.main();
StringBufferBenchmark.main();
StringBuilderBenchmark.main();
}