OILS / benchmarks / builtin-io.sh View on Github | oilshell.org

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