mirror of
https://github.com/dart-lang/sdk
synced 2024-10-03 00:09:20 +00:00
[benchmarks] Add typed data copy benchmark
This change adds TypedDataDuplicate benchmark which measures performance of making a copy of typed data lists. Issue: https://github.com/dart-lang/sdk/issues/40039 Change-Id: Iadabbe3ca42dab2d2567e36fb1acd83fc2bfeb0c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/132162 Commit-Queue: Alexander Markov <alexmarkov@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
parent
17fff9b721
commit
a20c9de3e8
118
benchmarks/TypedDataDuplicate/dart/TypedDataDuplicate.dart
Normal file
118
benchmarks/TypedDataDuplicate/dart/TypedDataDuplicate.dart
Normal file
|
@ -0,0 +1,118 @@
|
|||
// Copyright (c) 2020, 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.
|
||||
|
||||
// Micro-benchmarks for copying typed data lists.
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:benchmark_harness/benchmark_harness.dart';
|
||||
|
||||
abstract class Uint8ListCopyBenchmark extends BenchmarkBase {
|
||||
final int size;
|
||||
Uint8List input;
|
||||
Uint8List result;
|
||||
|
||||
Uint8ListCopyBenchmark(String method, this.size)
|
||||
: super('TypedDataDuplicate.Uint8List.$size.$method');
|
||||
|
||||
void setup() {
|
||||
input = Uint8List(size);
|
||||
for (int i = 0; i < size; ++i) {
|
||||
input[i] = (i + 3) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
void warmup() {
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
run();
|
||||
}
|
||||
}
|
||||
|
||||
void teardown() {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (result[i] != ((i + 3) & 0xff)) {
|
||||
throw 'Unexpected result';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Uint8ListCopyViaFromListBenchmark extends Uint8ListCopyBenchmark {
|
||||
Uint8ListCopyViaFromListBenchmark(int size) : super('fromList', size);
|
||||
|
||||
void run() {
|
||||
result = Uint8List.fromList(input);
|
||||
}
|
||||
}
|
||||
|
||||
class Uint8ListCopyViaLoopBenchmark extends Uint8ListCopyBenchmark {
|
||||
Uint8ListCopyViaLoopBenchmark(int size) : super('loop', size);
|
||||
|
||||
void run() {
|
||||
result = Uint8List(input.length);
|
||||
for (var i = 0; i < input.length; i++) {
|
||||
result[i] = input[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Float64ListCopyBenchmark extends BenchmarkBase {
|
||||
final int size;
|
||||
Float64List input;
|
||||
Float64List result;
|
||||
|
||||
Float64ListCopyBenchmark(String method, this.size)
|
||||
: super('TypedDataDuplicate.Float64List.$size.$method');
|
||||
|
||||
void setup() {
|
||||
input = Float64List(size);
|
||||
for (int i = 0; i < size; ++i) {
|
||||
input[i] = (i - 7).toDouble();
|
||||
}
|
||||
}
|
||||
|
||||
void teardown() {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (result[i] != (i - 7).toDouble()) {
|
||||
throw 'Unexpected result';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Float64ListCopyViaFromListBenchmark extends Float64ListCopyBenchmark {
|
||||
Float64ListCopyViaFromListBenchmark(int size) : super('fromList', size);
|
||||
|
||||
void run() {
|
||||
result = Float64List.fromList(input);
|
||||
}
|
||||
}
|
||||
|
||||
class Float64ListCopyViaLoopBenchmark extends Float64ListCopyBenchmark {
|
||||
Float64ListCopyViaLoopBenchmark(int size) : super('loop', size);
|
||||
|
||||
void run() {
|
||||
result = Float64List(input.length);
|
||||
for (var i = 0; i < input.length; i++) {
|
||||
result[i] = input[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
final sizes = [8, 32, 256, 16384];
|
||||
final benchmarks = [
|
||||
for (int size in sizes) ...[
|
||||
Uint8ListCopyViaLoopBenchmark(size),
|
||||
Uint8ListCopyViaFromListBenchmark(size)
|
||||
],
|
||||
for (int size in sizes) ...[
|
||||
Float64ListCopyViaLoopBenchmark(size),
|
||||
Float64ListCopyViaFromListBenchmark(size)
|
||||
]
|
||||
];
|
||||
for (var bench in benchmarks) {
|
||||
bench.report();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue