Fix Shell functions and aliases sexion in README

And the markdown language tags
This commit is contained in:
наб authored and David Peter committed 2022-10-13 22:43:57 +02:00
1 parent 779d4250c4
commit 32e80fb22c
1 file changed
+22 -23
+22 -23
View File
@@ -28,19 +28,19 @@ A command-line benchmarking tool.
To run a benchmark, you can simply call `hyperfine <command>...`. The argument(s) can be any To run a benchmark, you can simply call `hyperfine <command>...`. The argument(s) can be any
shell command. For example: shell command. For example:
``` bash ```sh
hyperfine 'sleep 0.3' hyperfine 'sleep 0.3'
``` ```
Hyperfine will automatically determine the number of runs to perform for each command. By default, Hyperfine will automatically determine the number of runs to perform for each command. By default,
it will perform *at least* 10 benchmarking runs and measure for at least 3 seconds. To change this, it will perform *at least* 10 benchmarking runs and measure for at least 3 seconds. To change this,
you can use the `-r`/`--runs` option: you can use the `-r`/`--runs` option:
``` bash ```sh
hyperfine --runs 5 'sleep 0.3' hyperfine --runs 5 'sleep 0.3'
``` ```
If you want to compare the runtimes of different programs, you can pass multiple commands: If you want to compare the runtimes of different programs, you can pass multiple commands:
``` bash ```sh
hyperfine 'hexdump file' 'xxd file' hyperfine 'hexdump file' 'xxd file'
``` ```
@@ -51,19 +51,19 @@ by disk caches and whether they are cold or warm.
If you want to run the benchmark on a warm cache, you can use the `-w`/`--warmup` option to If you want to run the benchmark on a warm cache, you can use the `-w`/`--warmup` option to
perform a certain number of program executions before the actual benchmark: perform a certain number of program executions before the actual benchmark:
``` bash ```sh
hyperfine --warmup 3 'grep -R TODO *' hyperfine --warmup 3 'grep -R TODO *'
``` ```
Conversely, if you want to run the benchmark for a cold cache, you can use the `-p`/`--prepare` Conversely, if you want to run the benchmark for a cold cache, you can use the `-p`/`--prepare`
option to run a special command before *each* timing run. For example, to clear harddisk caches option to run a special command before *each* timing run. For example, to clear harddisk caches
on Linux, you can run on Linux, you can run
``` bash ```sh
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
``` ```
To use this specific command with hyperfine, call `sudo -v` to temporarily gain sudo permissions To use this specific command with hyperfine, call `sudo -v` to temporarily gain sudo permissions
and then call: and then call:
``` bash ```sh
hyperfine --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' 'grep -R TODO *' hyperfine --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' 'grep -R TODO *'
``` ```
@@ -71,12 +71,12 @@ hyperfine --prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches' 'grep -R
If you want to run a series of benchmarks where a single parameter is varied (say, the number of If you want to run a series of benchmarks where a single parameter is varied (say, the number of
threads), you can use the `-P`/`--parameter-scan` option and call: threads), you can use the `-P`/`--parameter-scan` option and call:
``` bash ```sh
hyperfine --prepare 'make clean' --parameter-scan num_threads 1 12 'make -j {num_threads}' hyperfine --prepare 'make clean' --parameter-scan num_threads 1 12 'make -j {num_threads}'
``` ```
This also works with decimal numbers. The `-D`/`--parameter-step-size` option can be used This also works with decimal numbers. The `-D`/`--parameter-step-size` option can be used
to control the step size: to control the step size:
``` bash ```sh
hyperfine --parameter-scan delay 0.3 0.7 -D 0.2 'sleep {delay}' hyperfine --parameter-scan delay 0.3 0.7 -D 0.2 'sleep {delay}'
``` ```
This runs `sleep 0.3`, `sleep 0.5` and `sleep 0.7`. This runs `sleep 0.3`, `sleep 0.5` and `sleep 0.7`.
@@ -91,7 +91,7 @@ hyperfine -L compiler gcc,clang '{compiler} -O2 main.cpp'
By default, commands are executed using a predefined shell (`/bin/sh` on Unix, `cmd.exe` on Windows). By default, commands are executed using a predefined shell (`/bin/sh` on Unix, `cmd.exe` on Windows).
If you want to use a different shell, you can use the `-S, --shell <SHELL>` option: If you want to use a different shell, you can use the `-S, --shell <SHELL>` option:
``` bash ```sh
hyperfine --shell zsh 'for i in {1..10000}; do echo test; done' hyperfine --shell zsh 'for i in {1..10000}; do echo test; done'
``` ```
@@ -110,22 +110,21 @@ hyperfine -N 'grep TODO /home/user'
### Shell functions and aliases ### Shell functions and aliases
If you are using bash, you can export shell functions to directly benchmark them with hyperfine: Bash supports passing functions through the environment:
```
$ my_function() { sleep 1; }
$ export -f my_function
$ hyperfine my_function
```
If you are using a different shell, or if you want to benchmark shell aliases, you may try to put
them in a separate file:
```bash ```bash
echo 'my_function() { sleep 1 }' > /tmp/my_function.sh my_function() { sleep 1; }
export -f my_function
hyperfine --shell=bash my_function
```
Otherwise, inline them into or source them from the benchmarked program:
```sh
hyperfine 'my_function() { sleep 1; }; my_function'
echo 'alias my_alias="sleep 1"' > /tmp/my_alias.sh echo 'alias my_alias="sleep 1"' > /tmp/my_alias.sh
hyperfine 'source /tmp/my_function.sh; eval my_function' hyperfine '. /tmp/my_alias.sh; my_alias'
hyperfine 'source /tmp/my_alias.sh; eval my_alias'
``` ```
### Exporting results ### Exporting results
@@ -133,7 +132,7 @@ hyperfine 'source /tmp/my_alias.sh; eval my_alias'
Hyperfine has multiple options for exporting benchmark results to CSV, JSON, Markdown and other Hyperfine has multiple options for exporting benchmark results to CSV, JSON, Markdown and other
formats (see `--help` text for details). formats (see `--help` text for details).
#### Markdown #### Markdown
You can use the `--export-markdown <file>` option to create tables like the following: You can use the `--export-markdown <file>` option to create tables like the following: