12

Possible Duplicate:
Measure script execution time

How would I time how long it takes for my script foo.sh to run?

I'm looking for something akin to tic and toc in MATLAB.

dsg
  • 1,169
  • 2
  • 17
  • 25

2 Answers2

22

Easiest way is to use bash's integrated time, GNU Time or another unix time command implementation:

time ./sript.sh

If you're interested in ticks, you can approximate it with a little help from /proc/cpuinfo.

If you want to dig deeper, have a look at strace.

mbx
  • 764
  • 2
  • 12
  • 22
  • 2
    `time is a shell keyword` – Hello71 Apr 25 '11 at 23:21
  • 1
    @Hello71 my linux `bash` does not provide a time command, it uses `/usr/bin/time`. However `zsh` does, as well as my solaris `tcsh`. So even if it's not builtin your shell, you have a time provided by your userland as a fallback. Besides, those time commands may provide a slightly different output. – mbx Apr 26 '11 at 09:45
  • @mbx: [As of version 2.0 of Bash, time became a shell reserved word, with slightly altered behavior in a pipeline.](http://tldp.org/LDP/abs/html/timedate.html#TIMREF) – Hello71 Apr 26 '11 at 20:04
  • @Hello71 although reserved, on that ystem `which time` returns userland time instead of the reserved keyword warning – mbx Apr 26 '11 at 21:20
  • @mbx: `which` never does that. I'm referring to `type`. – Hello71 Apr 26 '11 at 21:21
  • @Hello71 not `which`, but the shell does (`zsh` and `tcsh`) – mbx Apr 26 '11 at 21:37
  • What if one wants to measure the time between multiple commands/lines? – normanius Jul 10 '19 at 21:52
5

time can achieve this. In this case:

$ time foo.sh             
J.K.
  • 236
  • 1
  • 1