Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure the shell script execution time in milliseconds on Mac OS

I was wondering if there is the way to get time in milliseconds from shell script on Mac OS.

I need it to time how much certain query runs.

Now I can only get the time in seconds:

Start=`date +%s`
End =`date +%s`
Time=$Start-$End
like image 500
user3344382 Avatar asked Mar 12 '14 08:03

user3344382


Video Answer


2 Answers

just use the 'time' command:

time something

something could be a shell, or a command (find, etc)

the "real" time is the total elapsed time you want, and includes milliseconds

like image 83
Olivier Dulac Avatar answered Nov 09 '22 07:11

Olivier Dulac


You could use the benchmarking-tool hyperfine (https://github.com/sharkdp/hyperfine).

It is more elaborate than time, by default runs your command multiple times and gives you mean runtime, deviation, min, and max.

Simple usage

hyperfine your_command

Result looks like this (result of hyperfine 'sleep 0.5'):

bash-3.2$ hyperfine 'sleep 0.5'
Benchmark #1: sleep 0.5
  Time (mean ± σ):     505.6 ms ±   1.5 ms    [User: 0.8 ms, System: 1.2 ms]
  Range (min … max):   503.1 ms … 508.8 ms    10 runs

There is one caveat, the minimum number of runs is 2 (hyperfine -r 2 'your command').

Installation

Hyperfine can be installed via Homebrew:

brew install hyperfine

For more info see: https://github.com/sharkdp/hyperfine#on-macos

like image 31
Dietrich Avatar answered Nov 09 '22 07:11

Dietrich