| 1 |
# Benchmarking cut |
| 2 |
|
| 3 |
## Performance profile |
| 4 |
|
| 5 |
In normal use cases a significant amount of the total execution time of `cut` |
| 6 |
is spent performing I/O. When invoked with the `-f` option (cut fields) some |
| 7 |
CPU time is spent on detecting fields (in `Searcher::next`). Other than that |
| 8 |
some small amount of CPU time is spent on breaking the input stream into lines. |
| 9 |
|
| 10 |
## How to |
| 11 |
|
| 12 |
When fixing bugs or adding features you might want to compare |
| 13 |
performance before and after your code changes. |
| 14 |
|
| 15 |
- `hyperfine` can be used to accurately measure and compare the total |
| 16 |
execution time of one or more commands. |
| 17 |
|
| 18 |
```shell |
| 19 |
cargo build --release --package uu_cut |
| 20 |
|
| 21 |
hyperfine -w3 "./target/release/cut -f2-4,8 -d' ' input.txt" "cut -f2-4,8 -d' ' input.txt" |
| 22 |
``` |
| 23 |
|
| 24 |
You can put those two commands in a shell script to be sure that you don't |
| 25 |
forget to build after making any changes. |
| 26 |
|
| 27 |
When optimizing or fixing performance regressions seeing the number of times a |
| 28 |
function is called, and the amount of time it takes can be useful. |
| 29 |
|
| 30 |
- `cargo flamegraph` generates flame graphs from function level metrics it records using `perf` or `dtrace` |
| 31 |
|
| 32 |
```shell |
| 33 |
cargo flamegraph --bin cut --package uu_cut -- -f1,3-4 input.txt > /dev/null |
| 34 |
``` |
| 35 |
|
| 36 |
## What to benchmark |
| 37 |
|
| 38 |
There are four different performance paths in `cut` to benchmark. |
| 39 |
|
| 40 |
- Byte ranges `-c`/`--characters` or `-b`/`--bytes` e.g. `cut -c 2,4,6-` |
| 41 |
- Byte ranges with output delimiters e.g. `cut -c 4- --output-delimiter=/` |
| 42 |
- Fields e.g. `cut -f -4` |
| 43 |
- Fields with output delimiters e.g. `cut -f 7-10 --output-delimiter=:` |
| 44 |
|
| 45 |
Choose a test input file with large number of lines so that program startup time does not significantly affect the benchmark. |