mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 03:36:59 +00:00
119 lines
2.8 KiB
Dart
119 lines
2.8 KiB
Dart
|
// 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();
|
||
|
}
|
||
|
}
|