1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Usage:
|
4 | # benchamrks/builtin-io.sh <function name>
|
5 |
|
6 | set -o nounset
|
7 | set -o pipefail
|
8 | set -o errexit
|
9 |
|
10 | readonly BIG=_tmp/1m_lines.txt
|
11 |
|
12 | setup() {
|
13 | seq 1000000 > $BIG
|
14 |
|
15 | }
|
16 |
|
17 | # 25 ms
|
18 | wc-big() {
|
19 | time wc -l $BIG
|
20 | }
|
21 |
|
22 | # bash takes 156 ms here! Significantly slower than 'wc'.
|
23 | # bin/osh in Python takes over 5 seconds!
|
24 | #
|
25 | # TODO:
|
26 | # - Make sure bin/osh in C++ is reasonably fast.
|
27 | # - Make sure a loop with read --line is reasonably fast.
|
28 |
|
29 | mapfile-big() {
|
30 | time mapfile < $BIG
|
31 | echo ${#MAPFILE[@]} # verify length
|
32 | }
|
33 |
|
34 | # Hm this isn't that fast either, about 100 ms.
|
35 | python-big() {
|
36 | time python -S -c '
|
37 | import sys
|
38 | i = 0
|
39 | for line in sys.stdin:
|
40 | i += 1
|
41 | print(i)
|
42 | ' < $BIG
|
43 | }
|
44 |
|
45 | bash-syscall() {
|
46 | # Shows that there are tons of read(0, 1) calls!
|
47 | seq 20 | strace -e read -- bash -c 'mapfile'
|
48 | }
|
49 |
|
50 | python-syscall() {
|
51 | # Does read(0, 4096). A saner way to read files
|
52 | seq 20 | strace -e read -- python -c '
|
53 | import sys
|
54 | for line in sys.stdin:
|
55 | print(line)
|
56 | '
|
57 | }
|
58 |
|
59 |
|
60 | "$@"
|