How to Measure the Length of Execution of a Selected Portion of Code in Bash

The aim of this page📝 is to share a simple way of measuring the duration of a selected portion of code execution in bash.

  1. I am using ms unit
  2. To get the absolute timestamp, use $(date +%s%N | cut -b1-13)
  • %s for seconds since the UNIX epoch
  • %N adding nanosecond portion
  • cut is selecting the first 13 fields of the nanosecond portion => milliseconds
  1. Get the absolute timestamp at the beginning of the program
  2. Get the absolute timestamp at the end of the program
  3. Subtract end - beginning to get the duration
  4. print it

…you could also do date +%s%3N

LINKS

--

--

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (snowplow.io) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com