Debian Codegraph @

src/uu/seq/BENCHMARKING.md

raw
1 # Benchmarking to measure performance
2
3 To compare the performance of the `uutils` version of `seq` with the
4 GNU version of `seq`, you can use a benchmarking tool like
5 [hyperfine][0]. On Ubuntu 18.04 or later, you can install `hyperfine` by
6 running
7
8 ```shell
9 sudo apt-get install hyperfine
10 ```
11
12 Next, build the `seq` binary under the release profile:
13
14 ```shell
15 cargo build --release -p uu_seq
16 ```
17
18 Finally, you can compare the performance of the two versions of `seq`
19 by running, for example,
20
21 ```shell
22 hyperfine -L seq seq,target/release/seq "{seq} 1000000"
23 ```
24
25 ## Interesting test cases
26
27 Performance characteristics may vary a lot depending on the parameters,
28 and if custom formatting is required. In particular, it does appear
29 that the GNU implementation is heavily optimized for positive integer
30 outputs (which is probably the most common use case for `seq`).
31
32 Specifying a format or fixed width will slow down the
33 execution a lot (~15-20 times on GNU `seq`):
34 ```shell
35 hyperfine -L seq seq,target/release/seq "{seq} -f%g 1000000"
36 hyperfine -L seq seq,target/release/seq "{seq} -w 1000000"
37 ```
38
39 Floating point increments, or any negative bound, also degrades the
40 performance (~10-15 times on GNU `seq`):
41 ```shell
42 hyperfine -L seq seq,./target/release/seq "{seq} 0 0.000001 1"
43 hyperfine -L seq seq,./target/release/seq "{seq} -100 1 1000000"
44 ```
45
46 It is also interesting to compare performance with large precision
47 format. But in this case, the output itself should also be compared,
48 as GNU `seq` may not provide the same precision (`uutils` version of
49 `seq` provides arbitrary precision, while GNU `seq` appears to be
50 limited to `long double` on the given platform, i.e. 64/80/128-bit
51 float):
52 ```shell
53 hyperfine -L seq seq,target/release/seq "{seq} -f%.30f 0 0.000001 1"
54 ```
55
56 ## Optimizations
57
58 ### Buffering stdout
59
60 The original `uutils` implementation of `seq` did unbuffered writes
61 to stdout, causing a large number of system calls (and therefore a large amount
62 of system time). Simply wrapping `stdout` in a `BufWriter` increased performance
63 by about 2 times for a floating point increment test case, leading to similar
64 performance compared with GNU `seq`.
65
66 ### Directly print strings
67
68 As expected, directly printing a string:
69 ```rust
70 stdout.write_all(separator.as_bytes())?
71 ```
72 is quite a bit faster than using format to do the same operation:
73 ```rust
74 write!(stdout, "{separator}")?
75 ```
76
77 The change above resulted in a ~10% speedup.
78
79 ### Fast increment path
80
81 When dealing with positive integer values (first/increment/last), and
82 the default format is used, we use a custom fast path that does arithmetic
83 on u8 arrays (i.e. strings), instead of repeatedly calling into
84 formatting format.
85
86 This provides _massive_ performance gains, in the order of 10-20x compared
87 with the default implementation, at the expense of some added code complexity.
88
89 Just from performance numbers, it is clear that GNU `seq` uses similar
90 tricks, but we are more liberal on when we use our fast path (e.g. large
91 increments are supported, equal width is supported). Our fast path
92 implementation gets within ~10% of `seq` performance when its fast
93 path is activated.
94
95 [0]: https://github.com/sharkdp/hyperfine