Skip to content

Commit 87b28bd

Browse files
author
Kenneth Endfinger
committed
Make the benchmark more useful.
1 parent 1cfdb1c commit 87b28bd

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

example/binary_benchmark.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "dart:typed_data";
44
import "dart:math";
55

66
main() {
7-
var byteList = new Uint8List(1024 * 1024 * 5);
7+
var byteList = new Uint8List(1024 * 1024 * 50);
88
var random = new Random();
99
for (var i = 0; i < byteList.lengthInBytes; i++) {
1010
byteList[i] = random.nextInt(255);
@@ -35,14 +35,40 @@ main() {
3535
unpack(encoded);
3636
}
3737

38+
var watch = new Stopwatch();
39+
watch.start();
40+
var times = <List<int>>[];
41+
for (var i = 1; i <= 5; i++) {
42+
times.add(go(input));
43+
}
44+
watch.stop();
45+
46+
var totalEncodeTime = 0;
47+
var totalDecodeTime = 0;
48+
for (var time in times) {
49+
totalEncodeTime += time[0];
50+
totalDecodeTime += time[1];
51+
}
52+
var avgEncodeTime = totalEncodeTime / times.length;
53+
var avgDecodeTime = totalDecodeTime / times.length;
54+
55+
print("Took an average of ${(avgEncodeTime / 1000).toStringAsFixed(2)}ms to encode.");
56+
print("Took an average of ${(avgDecodeTime / 1000).toStringAsFixed(2)}ms to decode.");
57+
}
58+
59+
List<int> go(input) {
60+
var out = <int>[];
3861
var watch = new Stopwatch();
3962
watch.start();
4063
var encoded = pack(input);
4164
watch.stop();
4265
print("Took ${watch.elapsedMicroseconds / 1000}ms to encode.");
66+
out.add(watch.elapsedMicroseconds);
4367
watch.reset();
4468
watch.start();
4569
var decoded = unpack(encoded);
4670
watch.stop();
4771
print("Took ${watch.elapsedMicroseconds / 1000}ms to decode.");
72+
out.add(watch.elapsedMicroseconds);
73+
return out;
4874
}

lib/src/stateful_packer.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class PackBuffer {
1212
class MsgPackBuffer implements PackBuffer {
1313
static const int defaultBufferSize = const int.fromEnvironment(
1414
"msgpack.packer.defaultBufferSize",
15-
defaultValue: 256
15+
defaultValue: 512
1616
);
1717

1818
List<Uint8List> _buffers = <Uint8List>[];
@@ -111,11 +111,11 @@ class MsgPackBuffer implements PackBuffer {
111111
@override
112112
Uint8List done() {
113113
Uint8List out = read();
114-
_buffers.length = 0;
115-
_buffer = null;
114+
_buffers = new List<Uint8List>();
116115
_len = 0;
117116
_totalLength = 0;
118117
_offset = 0;
118+
_buffer = null;
119119
return out;
120120
}
121121

0 commit comments

Comments
 (0)