Skip to content

Commit a20c9de

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[benchmarks] Add typed data copy benchmark
This change adds TypedDataDuplicate benchmark which measures performance of making a copy of typed data lists. Issue: dart-lang#40039 Change-Id: Iadabbe3ca42dab2d2567e36fb1acd83fc2bfeb0c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132162 Commit-Queue: Alexander Markov <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
1 parent 17fff9b commit a20c9de

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Micro-benchmarks for copying typed data lists.
6+
7+
import 'dart:typed_data';
8+
9+
import 'package:benchmark_harness/benchmark_harness.dart';
10+
11+
abstract class Uint8ListCopyBenchmark extends BenchmarkBase {
12+
final int size;
13+
Uint8List input;
14+
Uint8List result;
15+
16+
Uint8ListCopyBenchmark(String method, this.size)
17+
: super('TypedDataDuplicate.Uint8List.$size.$method');
18+
19+
void setup() {
20+
input = Uint8List(size);
21+
for (int i = 0; i < size; ++i) {
22+
input[i] = (i + 3) & 0xff;
23+
}
24+
}
25+
26+
void warmup() {
27+
for (int i = 0; i < 100; ++i) {
28+
run();
29+
}
30+
}
31+
32+
void teardown() {
33+
for (int i = 0; i < size; ++i) {
34+
if (result[i] != ((i + 3) & 0xff)) {
35+
throw 'Unexpected result';
36+
}
37+
}
38+
}
39+
}
40+
41+
class Uint8ListCopyViaFromListBenchmark extends Uint8ListCopyBenchmark {
42+
Uint8ListCopyViaFromListBenchmark(int size) : super('fromList', size);
43+
44+
void run() {
45+
result = Uint8List.fromList(input);
46+
}
47+
}
48+
49+
class Uint8ListCopyViaLoopBenchmark extends Uint8ListCopyBenchmark {
50+
Uint8ListCopyViaLoopBenchmark(int size) : super('loop', size);
51+
52+
void run() {
53+
result = Uint8List(input.length);
54+
for (var i = 0; i < input.length; i++) {
55+
result[i] = input[i];
56+
}
57+
}
58+
}
59+
60+
abstract class Float64ListCopyBenchmark extends BenchmarkBase {
61+
final int size;
62+
Float64List input;
63+
Float64List result;
64+
65+
Float64ListCopyBenchmark(String method, this.size)
66+
: super('TypedDataDuplicate.Float64List.$size.$method');
67+
68+
void setup() {
69+
input = Float64List(size);
70+
for (int i = 0; i < size; ++i) {
71+
input[i] = (i - 7).toDouble();
72+
}
73+
}
74+
75+
void teardown() {
76+
for (int i = 0; i < size; ++i) {
77+
if (result[i] != (i - 7).toDouble()) {
78+
throw 'Unexpected result';
79+
}
80+
}
81+
}
82+
}
83+
84+
class Float64ListCopyViaFromListBenchmark extends Float64ListCopyBenchmark {
85+
Float64ListCopyViaFromListBenchmark(int size) : super('fromList', size);
86+
87+
void run() {
88+
result = Float64List.fromList(input);
89+
}
90+
}
91+
92+
class Float64ListCopyViaLoopBenchmark extends Float64ListCopyBenchmark {
93+
Float64ListCopyViaLoopBenchmark(int size) : super('loop', size);
94+
95+
void run() {
96+
result = Float64List(input.length);
97+
for (var i = 0; i < input.length; i++) {
98+
result[i] = input[i];
99+
}
100+
}
101+
}
102+
103+
main() {
104+
final sizes = [8, 32, 256, 16384];
105+
final benchmarks = [
106+
for (int size in sizes) ...[
107+
Uint8ListCopyViaLoopBenchmark(size),
108+
Uint8ListCopyViaFromListBenchmark(size)
109+
],
110+
for (int size in sizes) ...[
111+
Float64ListCopyViaLoopBenchmark(size),
112+
Float64ListCopyViaFromListBenchmark(size)
113+
]
114+
];
115+
for (var bench in benchmarks) {
116+
bench.report();
117+
}
118+
}

0 commit comments

Comments
 (0)