-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.sh
executable file
·126 lines (94 loc) · 2.78 KB
/
benchmark.sh
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
#!/usr/bin/env zsh
set -eu
SCRIPT_DIT=$(cd "$(dirname "$0")"; pwd)
if [ $# = 0 ]; then
PACKAGE=".package(path: \"../\")"
else
PACKAGE=".package(url: \"https://github.com/yaslab/Hex.swift.git\", exact: \"$1\")"
fi
cd "$SCRIPT_DIT"
mkdir -p ./benchmark/Sources/benchmark
cd benchmark
cat << EOF > ./Package.swift
// swift-tools-version: 5.8
import PackageDescription
let package = Package(
name: "benchmark",
platforms: [.macOS(.v11)],
dependencies: [
$PACKAGE
],
targets: [
.executableTarget(
name: "benchmark",
dependencies: [.product(name: "Hex", package: "Hex.swift")])
]
)
EOF
cat << EOF > ./Sources/benchmark/main.swift
import Foundation
import Hex
let _length = 5120
let _loop = 25600
let _hexData = Data((0 ..< _length).map({ _ in 0x30 }))
let _hexString = String(repeating: "0", count: _length)
let _bytes = Data(count: _length)
print("method | time (s)")
print("--- | ---")
@inline(never)
func decodeData(_ n: Int, hexData: Data) {
let start = Date()
for _ in 0 ..< n {
_ = Data(hexEncoded: hexData)!
}
print("\`Data(hexEncoded: data)\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
@inline(never)
func decodeString(_ n: Int, hexString: String) {
let start = Date()
for _ in 0 ..< n {
_ = Data(hexEncoded: hexString)!
}
print("\`Data(hexEncoded: string)\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
@inline(never)
func encodeCollectionIntoData<T: Collection<UInt8>>(_ n: Int, bytes: T) {
let start = Date()
for _ in 0 ..< n {
_ = bytes.hexEncodedData()
}
print("\`Collection.hexEncodedData()\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
@inline(never)
func encodeCollectionIntoString<T: Collection<UInt8>>(_ n: Int, bytes: T) {
let start = Date()
for _ in 0 ..< n {
_ = bytes.hexEncodedString()
}
print("\`Collection.hexEncodedString()\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
@inline(never)
func encodeSequenceIntoData<T: Sequence<UInt8>>(_ n: Int, bytes: T) {
let start = Date()
for _ in 0 ..< n {
_ = bytes.hexEncodedData()
}
print("\`Sequence.hexEncodedData()\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
@inline(never)
func encodeSequenceIntoString<T: Sequence<UInt8>>(_ n: Int, bytes: T) {
let start = Date()
for _ in 0 ..< n {
_ = bytes.hexEncodedString()
}
print("\`Sequence.hexEncodedString()\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
decodeData(_loop, hexData: _hexData)
decodeString(_loop, hexString: _hexString)
encodeCollectionIntoData(_loop, bytes: _bytes)
encodeCollectionIntoString(_loop, bytes: _bytes)
encodeSequenceIntoData(_loop, bytes: _bytes)
encodeSequenceIntoString(_loop, bytes: _bytes)
EOF
swift package clean
swift run -c release