-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.sh
executable file
·61 lines (51 loc) · 1.33 KB
/
timing.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
#!/usr/bin/env bash
# Prints a Markdown table of the size and runtime of each solution.
accum=()
# Borrowed from bkt's benchmark script
avg_floats() {
python3 <(cat <<EOF
import sys
total = sum((float(arg) for arg in sys.argv[1:]))
print("{:.3f}".format(total/(len(sys.argv)-1)))
EOF
) "$@"
}
# Borrowed from bkt's benchmark script
sum_floats() {
python3 <(cat <<EOF
import sys
total = sum((float(arg) for arg in sys.argv[1:]))
print("{:.3f}".format(total))
EOF
) "$@"
}
# Borrowed from bash-cache's bc::_time
runtime() {
(
TIMEFORMAT=%R
time "$@" &> /dev/null
) 2>&1
}
row() {
local binary="target/release/${1}" binary_size runtimes=() avg_runtime
binary_size=$(stat -c %s "$binary" 2>/dev/null) || return 0 # skip missing binaries
for i in {0..3}; do
runtimes+=("$(runtime "$binary")")
done
# don't count the first run (:1 syntax) as it can be slower on some systems
avg_runtime=$(avg_floats "${runtimes[@]:1}")
accum+=("$avg_runtime")
printf '| %12s ' \
"${1#0}" \
"$(( binary_size / 1024 ))KB" \
"${avg_runtime}s" \
"$(sum_floats "${accum[@]}")s"
echo '|'
}
cargo clean --release
cargo build --release
printf '| %12s ' "Day" "Binary Size" "Runtime" "Accumulated"; echo '|'
echo '|--------------|--------------|--------------|--------------|'
for i in {01..25}; do
row "$i"
done