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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:math' as math;
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:vector_math/vector_math_64.dart';
import '../common.dart';
const int _kNumIterations = 1000000;
const int _kNumWarmUp = 10000;
void main() {
assert(false, "Don't run benchmarks in checked mode! Use 'flutter run --release'.");
print('MatrixUtils.transformRect and .transformPoint benchmark...');
Matrix4 _makePerspective(double radius, double angle, double perspective) {
return MatrixUtils.createCylindricalProjectionTransform(
radius: radius,
angle: angle,
perspective: perspective,
);
}
final List<Matrix4> _affineTransforms = <Matrix4>[
Matrix4.identity()..scale(1.2, 1.3, 1.0)..rotateZ(0.1),
Matrix4.identity()..translate(12.0, 13.0, 10.0),
Matrix4.identity()..scale(1.2, 1.3, 1.0)..translate(12.0, 13.0, 10.0),
];
final List<Matrix4> _perspectiveTransforms = <Matrix4>[
_makePerspective(10.0, math.pi / 8.0, 0.3),
_makePerspective( 8.0, math.pi / 8.0, 0.2),
_makePerspective( 1.0, math.pi / 4.0, 0.1)..rotateX(0.1),
];
final List<Rect> _rectangles = <Rect>[
const Rect.fromLTRB(1.1, 1.2, 1.5, 1.8),
const Rect.fromLTRB(1.1, 1.2, 0.0, 1.0),
const Rect.fromLTRB(1.1, 1.2, 1.3, 1.0),
const Rect.fromLTRB(-1.1, -1.2, 0.0, 1.0),
const Rect.fromLTRB(-1.1, -1.2, -1.5, -1.8),
];
final List<Offset> _offsets = <Offset>[
const Offset(1.1, 1.2),
const Offset(1.5, 1.8),
const Offset(0.0, 0.0),
const Offset(-1.1, -1.2),
const Offset(-1.5, -1.8),
];
final int nAffine = _affineTransforms.length;
final int nPerspective = _perspectiveTransforms.length;
final int nRectangles = _rectangles.length;
final int nOffsets = _offsets.length;
// Warm up lap
for (int i = 0; i < _kNumWarmUp; i += 1) {
final Matrix4 transform = _perspectiveTransforms[i % nPerspective];
final Rect rect = _rectangles[(i ~/ nPerspective) % nRectangles];
final Offset offset = _offsets[(i ~/ nPerspective) % nOffsets];
MatrixUtils.transformRect(transform, rect);
MatrixUtils.transformPoint(transform, offset);
}
for (int i = 0; i < _kNumWarmUp; i += 1) {
final Matrix4 transform = _affineTransforms[i % nAffine];
final Rect rect = _rectangles[(i ~/ nAffine) % nRectangles];
final Offset offset = _offsets[(i ~/ nAffine) % nOffsets];
MatrixUtils.transformRect(transform, rect);
MatrixUtils.transformPoint(transform, offset);
}
final Stopwatch watch = Stopwatch();
watch.start();
for (int i = 0; i < _kNumIterations; i += 1) {
final Matrix4 transform = _perspectiveTransforms[i % nPerspective];
final Rect rect = _rectangles[(i ~/ nPerspective) % nRectangles];
MatrixUtils.transformRect(transform, rect);
}
watch.stop();
final int rectMicrosecondsPerspective = watch.elapsedMicroseconds;
watch.reset();
watch.start();
for (int i = 0; i < _kNumIterations; i += 1) {
final Matrix4 transform = _affineTransforms[i % nAffine];
final Rect rect = _rectangles[(i ~/ nAffine) % nRectangles];
MatrixUtils.transformRect(transform, rect);
}
watch.stop();
final int rectMicrosecondsAffine = watch.elapsedMicroseconds;
watch.reset();
watch.start();
for (int i = 0; i < _kNumIterations; i += 1) {
final Matrix4 transform = _perspectiveTransforms[i % nPerspective];
final Offset offset = _offsets[(i ~/ nPerspective) % nOffsets];
MatrixUtils.transformPoint(transform, offset);
}
watch.stop();
final int pointMicrosecondsPerspective = watch.elapsedMicroseconds;
watch.reset();
watch.start();
for (int i = 0; i < _kNumIterations; i += 1) {
final Matrix4 transform = _affineTransforms[i % nAffine];
final Offset offset = _offsets[(i ~/ nAffine) % nOffsets];
MatrixUtils.transformPoint(transform, offset);
}
watch.stop();
final int pointMicrosecondsAffine = watch.elapsedMicroseconds;
final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
const double scale = 1000.0 / _kNumIterations;
printer.addResult(
description: 'MatrixUtils.transformRectPerspective',
value: rectMicrosecondsPerspective * scale,
unit: 'ns per iteration',
name: 'MatrixUtils_persp_transformRect_iteration',
);
printer.addResult(
description: 'MatrixUtils.transformRectAffine',
value: rectMicrosecondsAffine * scale,
unit: 'ns per iteration',
name: 'MatrixUtils_affine_transformRect_iteration',
);
printer.addResult(
description: 'MatrixUtils.transformPointPerspective',
value: pointMicrosecondsPerspective * scale,
unit: 'ns per iteration',
name: 'MatrixUtils_persp_transformPoint_iteration',
);
printer.addResult(
description: 'MatrixUtils.transformPointAffine',
value: pointMicrosecondsAffine * scale,
unit: 'ns per iteration',
name: 'MatrixUtils_affine_transformPoint_iteration',
);
printer.printToStdout();
}