ref: 5e945955e2c3c1c0843786a31e890f5bfbe82ffb blog.craftyguy.net/content/2021-12-30-shell-script-perf.md -rw-r--r-- 1.0 KiB
5e945955Clayton Craft content/2022-01-07-ipxe-aarch64: new post 7 months ago

+++ title = "Timing performance of functions in shell scripts" date = 2021-12-30 [taxonomies] tags = ["shell", "linux", "benchmarking"] +++

I recently started looking at some shell scripts that run fine on big fast powerful systems (i.e. x86 laptops/desktops), but quite slowly on small slow devices. Using time to run the script (or, the amazing hyperfine) works OK if you're timing the entire script execution, but what do you do if you want to time individual functions within the script?

Well without getting too fancy, I came up with the following, which is capable of timing far below 1 second:


foo() {
    # do real work
    sleep 4

start=$(date +%s.%N)


end=$(date +%s.%N)
echo "foo: $( echo "$end - $start" | bc -l ) seconds..."

It's not the most accurate thing in the world, and you'll pay some penalty for running date in a sub shell, twice, but it works well for blaming slowdowns in a shell script.

$ ./run.sh
foo: 4.037054796 seconds...