Debian Codegraph @

src/uu/wc/BENCHMARKING.md

raw
1 # Benchmarking wc
2
3 <!-- spell-checker:ignore (words) uuwc uucat largefile somefile Mshortlines moby lwcm cmds tablefmt -->
4
5 Much of what makes wc fast is avoiding unnecessary work. It has multiple strategies,
6 depending on which data is requested.
7
8 ## Strategies
9
10 ### Counting bytes
11
12 In the case of `wc -c` the content of the input doesn't have to be inspected at all,
13 only the size has to be known. That enables a few optimizations.
14
15 #### File size
16
17 If it can, wc reads the file size directly. This is not interesting to benchmark,
18 except to see if it still works. Try `wc -c largefile`.
19
20 #### `splice()`
21
22 On Linux `splice()` is used to get the input's length while discarding it directly.
23
24 The best way I've found to generate a fast input to test `splice()` is to pipe the
25 output of uutils `cat` (with `splice()` support) into it. If a file is given as its input directly (as in
26 `wc -c < largefile`) the first strategy kicks in. Try `uucat somefile | wc -c`.
27
28 ### Counting lines and UTF-8 characters
29
30 If the flags set are a subset of `-clm` then the input doesn't have to be decoded. The
31 input is read in chunks and the `bytecount` crate is used to count the newlines (`-l` flag)
32 and/or UTF-8 characters (`-m` flag).
33
34 It's useful to vary the line length in the input. GNU wc seems particularly
35 bad at short lines.
36
37 ### Processing unicode
38
39 This is the most general strategy, and it's necessary for counting words,
40 characters, and line lengths. Individual steps are still switched on and off
41 depending on what must be reported.
42
43 Try varying which of the `-w`, `-m`, `-l` and `-L` flags are used.
44 (The `-c` flag is unlikely to make a difference.)
45
46 Passing no flags is equivalent to passing `-wcl`. That case should perhaps be
47 given special attention as it's the default.
48
49 ## Generating files
50
51 To generate a file with many very short lines, run
52 `yes | head -c50000000 > 25Mshortlines`.
53
54 To get a file with less artificial contents, download a book from
55 Project Gutenberg and concatenate it a lot of times:
56
57 ```shell
58 curl https://www.gutenberg.org/files/2701/2701-0.txt -o moby.txt
59 cat moby.txt moby.txt moby.txt moby.txt > moby4.txt
60 cat moby4.txt moby4.txt moby4.txt moby4.txt > moby16.txt
61 cat moby16.txt moby16.txt moby16.txt moby16.txt > moby64.txt
62 ```
63
64 And get one with lots of unicode too:
65
66 ```shell
67 curl https://www.gutenberg.org/files/30613/30613-0.txt -o odyssey.txt
68 cat odyssey.txt odyssey.txt odyssey.txt odyssey.txt > odyssey4.txt
69 cat odyssey4.txt odyssey4.txt odyssey4.txt odyssey4.txt > odyssey16.txt
70 cat odyssey16.txt odyssey16.txt odyssey16.txt odyssey16.txt > odyssey64.txt
71 cat odyssey64.txt odyssey64.txt odyssey64.txt odyssey64.txt > odyssey256.txt
72 ```
73
74 Finally, it's interesting to try a binary file. Look for one with
75 `du -sh /usr/bin/* | sort -h`. On my system `/usr/bin/docker` is a good
76 candidate as it's fairly large.
77
78 ## Running benchmarks
79
80 Use [`hyperfine`](https://github.com/sharkdp/hyperfine) to compare the
81 performance. For example, `hyperfine 'wc somefile' 'uuwc somefile'`.
82
83 If you want to get fancy and exhaustive, generate a table:
84
85 | | moby64.txt | odyssey256.txt | 25Mshortlines | /usr/bin/docker |
86 |-------------------------|--------------|------------------|-----------------|-------------------|
87 | `wc <FILE>` | 1.3965 | 1.6182 | 5.2967 | 2.2294 |
88 | `wc -c <FILE>` | 0.8134 | 1.2774 | 0.7732 | 0.9106 |
89 | `uucat <FILE> \| wc -c` | 2.7760 | 2.5565 | 2.3769 | 2.3982 |
90 | `wc -l <FILE>` | 1.1441 | 1.2854 | 2.9681 | 1.1493 |
91 | `wc -L <FILE>` | 2.1087 | 1.2551 | 5.4577 | 2.1490 |
92 | `wc -m <FILE>` | 2.7272 | 2.1704 | 7.3371 | 3.4347 |
93 | `wc -w <FILE>` | 1.9007 | 1.5206 | 4.7851 | 2.8529 |
94 | `wc -lwcmL <FILE>` | 1.1687 | 0.9169 | 4.4092 | 2.0663 |
95
96 Beware that:
97
98 - Results are fuzzy and change from run to run
99 - You'll often want to check versions of uutils wc against each other instead
100 of against GNU
101 - This takes a lot of time to generate
102 - This only shows the relative speedup, not the absolute time, which may be
103 misleading if the time is very short
104
105 Created by the following Python script:
106
107 ```python
108 import json
109 import subprocess
110
111 from tabulate import tabulate
112
113 bins = ["wc", "uuwc"]
114 files = ["moby64.txt", "odyssey256.txt", "25Mshortlines", "/usr/bin/docker"]
115 cmds = [
116 "{cmd} {file}",
117 "{cmd} -c {file}",
118 "uucat {file} | {cmd} -c",
119 "{cmd} -l {file}",
120 "{cmd} -L {file}",
121 "{cmd} -m {file}",
122 "{cmd} -w {file}",
123 "{cmd} -lwcmL {file}",
124 ]
125
126 table = []
127 for cmd in cmds:
128 row = ["`" + cmd.format(cmd="wc", file="<FILE>") + "`"]
129 for file in files:
130 subprocess.run(
131 [
132 "hyperfine",
133 cmd.format(cmd=bins[0], file=file),
134 cmd.format(cmd=bins[1], file=file),
135 "--export-json=out.json",
136 ],
137 check=True,
138 )
139 with open("out.json") as f:
140 res = json.load(f)["results"]
141 row.append(round(res[0]["mean"] / res[1]["mean"], 4))
142 table.append(row)
143 print(tabulate(table, [""] + files, tablefmt="github"))
144 ```
145
146 (You may have to adjust the `bins` and `files` variables depending on your
147 setup, and please do add other interesting cases to `cmds`.)