Measure time easily in shell

Hi all,

This probably doesn't really qualify as a "project", and it's mostly useful to people writing shell code.

But anyway, here is a small library for measuring elapsed time based on /proc/uptime.

Useful for quick performance profiling, written in POSIX-compliant syntax and has no dependencies except for Linux (so works on basically any Linux system, including OpenWrt), fast and with proper error handling.

This was originally written for adblock-lean, then improved and ported into geoip-shell (I will contribute the improvements back into adblock-lean), and now published as a standalone library.

Enjoy!

(yes, there is a built-in utility for measuring time, a.k.a. /usr/bin/time, but it can not be easily employed to measure time which a certain part of your script is taking)

6 Likes

P.s. I wonder if you guys find this useful? There is a lot of reusable utility shell code I've written over time, and sometimes I'm wondering if it makes sense to release some particular part of it as a standalone library but I'm never sure if anyone else would be actually interested in it. So a bit of feedback (not just thumbs-up) would be helpful.

Hi @antonk
This does look useful. Seems like a nice lightweight way to get sub-second timings with the centisecond support, or even whole second precision rather than calling a binary like date. I'll try it in my current project.

As it wasn't in my recent usage, I had to search for how to use functions in a different file and found "include" and "dot file". I just tried it in my interactive shell and it worked for me. :=) I had used dot files 10+ years ago so it makes sense to me.

It might be helpful to add a sentence or two about including the functions with dot file syntax or some brief clue.

Thanks for sharing it!