Skip to content

Instantly share code, notes, and snippets.

@SnapperGee
Created January 7, 2024 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SnapperGee/ef96ede9bccb35b103bf14d973833da1 to your computer and use it in GitHub Desktop.
Save SnapperGee/ef96ede9bccb35b103bf14d973833da1 to your computer and use it in GitHub Desktop.
sed vs tail-cut benchmark

sed vs tail-cut Benchmark

Benchmark to compare the performance of either using sed or tail and cut to modify input.

Usage

Running the following command will run the benchmark.

bash run_benchmarks.sh # or bash ./run_benchmarks.sh

Scripts being tested

The 2 following scripts achieve the same result of removing the first 2 lines of input and then removing everything after (and including) the first space on each remaining line. However, one uses sed while the other uses a combo of tail and cut.

  • sed '1,2d; s/ .*//' "$1"

  • tail -n +3 "$1" | cut -d ' ' -f1

#!/usr/bin/env bash
benchmark_sed() {
time for i in {1..5000}; do
sed '1,2d; s/ .*//' "$1" > /dev/null
done
}
benchmark_sed "$1"
#!/usr/bin/env bash
benchmark_tailcut() {
time for i in {1..5000}; do
tail -n +3 "$1" | cut -d ' ' -f1 > /dev/null
done
}
benchmark_tailcut "$1"
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Line 1
Line 2
Hello World
Sample text
Another example
Copyright 2024 Snap <https://github.com/SnapperGee>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
INPUT="$DIR/input.txt"
printf 'sed:\n------------------'
source "$DIR/benchmark_sed.sh" "$INPUT"
printf '\ntail-cut:\n------------------'
source "$DIR/benchmark_tailcut.sh" "$INPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment