Created
April 1, 2011 09:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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