forked from koraa/huniq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.sh
executable file
·112 lines (97 loc) · 2.24 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
#! /usr/bin/env bash
set -e
trap "exit" SIGINT SIGTERM # exit from loop
cd "$(dirname "$0")"
huniq2bin="./target/release/huniq"
huniq1dir="./target/benchmark/huniq1"
huniq1bin="${huniq1dir}/huniq"
measure() {
env time -f'%e %M' "$@"
}
meas_datamash() {
if [[ "$@" = "" ]]; then
{ measure datamash -s groupby 1 first 1 >/dev/null; } 2>&1
else
{ measure datamash -s groupby 1 count 1 >/dev/null; } 2>&1
fi
}
meas_awk() {
local cmd="$1"; shift
if [[ "$@" = "" ]]; then
{ measure "$cmd" '!visited[$0]++' >/dev/null; } 2>&1
else
{
measure "$cmd" '
{
visited[$0]++;
}
END {
for (k in visited)
print(k, visited[k]);
}' >/dev/null
} 2>&1
fi
}
meas_shell() {
if [[ "$@" = "" ]]; then
{ measure sort -u >/dev/null; } 2>&1
else
{
measure sort | measure uniq -c >/dev/null
} 2>&1 | awk '
{
elapsed=$1;
mem+=$2;
}
END {
print(elapsed, mem);
}'
fi
}
meas_exe() {
{ measure "$@" >/dev/null; } 2>&1
}
bench() {
local name="$1"; shift
yes | head -n "$repeats" \
| while read _; do cat /usr/share/dict/*; done \
| "$@" ${modeargs[${mode}]} \
| while read results; do echo "$mode $repeats $name $results"; done
}
main() {
test -e "$huniq2bin" || {
cargo build --release
}
test -e "$huniq1dir" || {
git clone --recursive "https://github.com/SoftwearDevelopment/huniq" "$huniq1dir"
} >&2
test -e "$huniq1bin" || {
cd "$huniq1dir"
make
cd -
} >&2
declare -A modeargs
modeargs[uniq]=""
modeargs[count]="-c"
while true; do
for mode in "uniq" "count"; do
for repeats in 1 2 5 10 50 100; do
bench 'huniq2-rust' meas_exe "${huniq2bin}"
bench 'huniq1-c++ ' meas_exe "${huniq1bin}"
bench 'awk-sys ' meas_awk awk
if which gawk 2>/dev/null >/dev/null; then
bench 'gawk ' meas_awk gawk
fi
if which nawk 2>/dev/null >/dev/null; then
bench 'nawk ' meas_awk nawk
fi
if which datamash 2>/dev/null >/dev/null; then
bench 'datamash ' meas_datamash
fi
bench 'shell ' meas_shell
echo
done
done
done
}
main