Skip to content

Instantly share code, notes, and snippets.

@zandev
Created April 1, 2011 09:55
Show Gist options
  • Select an option

  • Save zandev/897960 to your computer and use it in GitHub Desktop.

Select an option

Save zandev/897960 to your computer and use it in GitHub Desktop.
benchmarking some ways to extract the first word of a string in bash
#!/bin/bash
iuser="boby me me and me"
pipe_it() {
echo "$iuser" | awk '{print $1}'
}
heredoc_it() {
awk '{print $1}' <<< "$iuser"
}
substitute_it() {
echo "${iuser%% *}"
}
bench_with() {
echo
echo "$1":
time for n in {1..1000}; do "$1" > /dev/null; done
}
bench_with 'pipe_it'
bench_with 'heredoc_it'
bench_with 'substitute_it'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment