Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell aliases and functions? #270

Closed
for-coursera opened this issue Feb 22, 2020 · 2 comments · Fixed by #343
Closed

Shell aliases and functions? #270

for-coursera opened this issue Feb 22, 2020 · 2 comments · Fixed by #343
Labels
documentation good first issue Good for newcomers question Further information is requested

Comments

@for-coursera
Copy link

It can't test shell aliases and function, can it? :)

@sharkdp
Copy link
Owner

sharkdp commented Feb 22, 2020

bash

Bash functions can easily be benchmarked by exporting them:

$ sleep1() { sleep 1; }
$ export -f sleep1
$ hyperfine sleep1
Benchmark #1: sleep1
  Time (mean ± σ):      1.005 s ±  0.003 s    [User: 3.4 ms, System: 1.7 ms]
  Range (min … max):    1.001 s …  1.009 s    10 runs

As for aliases, I don't think there is a way to export them. But you can always put the alias or shell function into a separate file:

echo 'alias my_alias="sleep 1"' > /tmp/my_alias.sh
hyperfine 'source /tmp/my_alias.sh; eval my_alias'

Note that this needs to be an absolute path and that you have to use eval to expand my_alias only after my_alias.sh has been parsed.

zsh

In zsh, exporting functions is not supported (by design). You would have to put the function or alias in a file as well:

echo 'my_function() { sleep 1 }' > my_function.zsh
hyperfine --shell zsh 'source my_function.zsh; eval my_function'

We should probably document this somewhere in the README

@sharkdp sharkdp added documentation question Further information is requested labels Feb 22, 2020
@for-coursera
Copy link
Author

Thanks! (I'm not closing this since you mentioned probably adding these hints to the documentation but feel free to close if needed.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants