Skip to content

Commit fec65a3

Browse files
committed
Add benchmarks for ByteReader
1 parent 7ca90eb commit fec65a3

4 files changed

Lines changed: 124 additions & 2 deletions

File tree

BitByteData.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
06ADF4CB214BB86600B8F0E9 /* ByteReaderBenchmarks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06ADF4CA214BB86600B8F0E9 /* ByteReaderBenchmarks.swift */; };
1011
06F065751FFAEA8700312A82 /* LsbBitReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F065571FFAEA1F00312A82 /* LsbBitReader.swift */; };
1112
06F065761FFAEA8700312A82 /* BitReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F065581FFAEA1F00312A82 /* BitReader.swift */; };
1213
06F065771FFAEA8700312A82 /* ByteReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F065591FFAEA1F00312A82 /* ByteReader.swift */; };
@@ -35,6 +36,7 @@
3536

3637
/* Begin PBXFileReference section */
3738
062BE3301FFBB9E300343CAD /* LinuxMain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinuxMain.swift; sourceTree = "<group>"; };
39+
06ADF4CA214BB86600B8F0E9 /* ByteReaderBenchmarks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ByteReaderBenchmarks.swift; sourceTree = "<group>"; };
3840
06F065571FFAEA1F00312A82 /* LsbBitReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LsbBitReader.swift; sourceTree = "<group>"; };
3941
06F065581FFAEA1F00312A82 /* BitReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BitReader.swift; sourceTree = "<group>"; };
4042
06F065591FFAEA1F00312A82 /* ByteReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ByteReader.swift; sourceTree = "<group>"; };
@@ -105,6 +107,7 @@
105107
06F065631FFAEA1F00312A82 /* MsbBitReaderTests.swift */,
106108
06F065641FFAEA1F00312A82 /* LsbBitWriterTests.swift */,
107109
06F065621FFAEA1F00312A82 /* MsbBitWriterTests.swift */,
110+
06ADF4CA214BB86600B8F0E9 /* ByteReaderBenchmarks.swift */,
108111
);
109112
path = BitByteDataTests;
110113
sourceTree = "<group>";
@@ -211,6 +214,7 @@
211214
isa = PBXSourcesBuildPhase;
212215
buildActionMask = 2147483647;
213216
files = (
217+
06ADF4CB214BB86600B8F0E9 /* ByteReaderBenchmarks.swift in Sources */,
214218
06F0658F1FFAEAA900312A82 /* LsbBitReaderTests.swift in Sources */,
215219
06F065901FFAEAA900312A82 /* ByteReaderTests.swift in Sources */,
216220
06F0658C1FFAEAA900312A82 /* MsbBitWriterTests.swift in Sources */,

Sources/ByteReader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class ByteReader {
7676
public func int(fromBytes count: Int) -> Int {
7777
precondition(count >= 0)
7878
precondition(bytesLeft >= count)
79-
// TODO: If uintX() could be force inlined or something in the future than probably it would make sense
79+
// TODO: If uintX() could be force inlined or something in the future then probably it would make sense
8080
// to use them for `count` == 2, 4 or 8.
8181
var result = 0
8282
for i in 0..<count {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (c) 2018 Timofey Solomko
2+
// Licensed under MIT License
3+
//
4+
// See LICENSE for license information
5+
6+
import XCTest
7+
import BitByteData
8+
9+
class ByteReaderBenchmarks: XCTestCase {
10+
11+
func testByte() {
12+
self.measure {
13+
let byteReader = ByteReader(data: Data(count: 10_485_760)) // 10 MB
14+
15+
for _ in 0..<5_000_000 {
16+
_ = byteReader.byte()
17+
}
18+
}
19+
}
20+
21+
func testBytes() {
22+
self.measure {
23+
let byteReader = ByteReader(data: Data(count: 10_485_760)) // 10 MB
24+
25+
for _ in 0..<500_000 {
26+
_ = byteReader.bytes(count: 20)
27+
}
28+
}
29+
}
30+
31+
func testIntFromBytes() {
32+
self.measure {
33+
let byteReader = ByteReader(data: Data(count: 10_485_760)) // 10 MB
34+
35+
for _ in 0..<1_000_000 {
36+
_ = byteReader.int(fromBytes: 7)
37+
}
38+
}
39+
}
40+
41+
func testUint16() {
42+
self.measure {
43+
let byteReader = ByteReader(data: Data(count: 10_485_760)) // 10 MB
44+
45+
for _ in 0..<1_000_000 {
46+
_ = byteReader.uint16()
47+
}
48+
}
49+
}
50+
51+
func testUint16FromBytes() {
52+
self.measure {
53+
let byteReader = ByteReader(data: Data(count: 10_485_760)) // 10 MB
54+
55+
for _ in 0..<2_000_000 {
56+
_ = byteReader.uint16(fromBytes: 1)
57+
}
58+
}
59+
}
60+
61+
func testUint32() {
62+
self.measure {
63+
let byteReader = ByteReader(data: Data(count: 10_485_760)) // 10 MB
64+
65+
for _ in 0..<500_000 {
66+
_ = byteReader.uint32()
67+
}
68+
}
69+
}
70+
71+
func testUint32FromBytes() {
72+
self.measure {
73+
let byteReader = ByteReader(data: Data(count: 10_485_760)) // 10 MB
74+
75+
for _ in 0..<500_000 {
76+
_ = byteReader.uint32(fromBytes: 3)
77+
}
78+
}
79+
}
80+
81+
func testUint64() {
82+
self.measure {
83+
let byteReader = ByteReader(data: Data(count: 10_485_760)) // 10 MB
84+
85+
for _ in 0..<1_00_000 {
86+
_ = byteReader.uint64()
87+
}
88+
}
89+
}
90+
91+
func testUint64FromBytes() {
92+
self.measure {
93+
let byteReader = ByteReader(data: Data(count: 10_485_760)) // 10 MB
94+
95+
for _ in 0..<1_000_000 {
96+
_ = byteReader.uint64(fromBytes: 7)
97+
}
98+
}
99+
}
100+
101+
}

Tests/LinuxMain.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,27 @@ extension MsbBitWriterTests {
108108
}
109109
}
110110

111+
extension ByteReaderBenchmarks {
112+
static var allTests: [(String, (ByteReaderBenchmarks) -> () -> Void)] {
113+
return [
114+
("testByte", testByte),
115+
("testBytes", testBytes),
116+
("testIntFromBytes", testIntFromBytes),
117+
("testUint16", testUint16),
118+
("testUint16FromBytes", testUint16FromBytes),
119+
("testUint32", testUint32),
120+
("testUint32FromBytes", testUint32FromBytes),
121+
("testUint64", testUint64),
122+
("testUint64FromBytes", testUint64FromBytes)
123+
]
124+
}
125+
}
126+
111127
XCTMain([
112128
testCase(ByteReaderTests.allTests),
113129
testCase(LsbBitReaderTests.allTests),
114130
testCase(MsbBitReaderTests.allTests),
115131
testCase(LsbBitWriterTests.allTests),
116-
testCase(MsbBitWriterTests.allTests)
132+
testCase(MsbBitWriterTests.allTests),
133+
testCase(ByteReaderBenchmarks.allTests)
117134
])

0 commit comments

Comments
 (0)