• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1886
  • Last Modified:

Bash script: How can I measure how long the script takes to run without doing anything outside the script?

Hi,

How can I achieve these two things in a bash script:

1) Measure and echo how long individual functions inside the script take to run

2) Measure and echo how long the entire script takes to run

**Note: I would like to achieve these without having to do anything outside the script if possible.  I only want to have this main script.  I don't want to have to keep track of a script to call this script etc.

Thanks,
0
tate1615
Asked:
tate1615
2 Solutions
 
omarfaridCommented:
you may use the command time for this

time scriptname
0
 
tate1615Author Commented:
Right, I could use the command line.
But how can I achieve these time measurements within the script?

For example, The script will output to a logfile:
Function X took 3 seconds
Function Y took 5 minutes
Script Finished! Script took 22 minutes
0
 
ozoCommented:
in a tcsh script, you could
set time=0
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
TintinCommented:
Here's a primitive way of doing it
#!/bin/bash
script_start=$(date +%s)
LOG=time.log
 
 
duration()
{
  start=$(date +%s)
  $*
  finish=$(date +%s)
  let total="$finish - $start"
  echo "Function $1 took $total seconds" >>$LOG
}
 
foo()
{
  echo foo
  sleep 2
}
 
duration foo
 
script_finish=$(date +%s)
let total="$finish - $start"
echo "Script Finished!  Script took $total seconds" >>$LOG

Open in new window

0
 
ghostdog74Commented:
you can use time in your script.

foo()
{
  echo foo
  sleep 2
}
time foo

Open in new window

0
 
TintinCommented:
I was going to suggest 'time', but the output isn't really what is required.  On Linux systems, /usr/bin/time (as opposed to the bash time builtin) is very flexible with the time output, but unfortunately you can't use it to time functions.
0
 
ghostdog74Commented:
why not, since time is built in to bash?  IMO, i think calling external command like date is not accurate at all.
  foo()
    {
    echo foo
    ls -l
    }
 
 
time {
 foo
} 
 

Open in new window

0
 
TintinCommented:
As previously mentioned, the bash time builtin doesn't have any options to format the output, whereas the time executable does, but the time executable won't recognise shell functions.  Run the following code to see what I mean.


#!/bin/bash
 
foo()
{
  echo foo
  sleep 1
}
 
 
time foo
/usr/bin/time -f "Function foo took %e to run" foo

Open in new window

0
 
ghostdog74Commented:

foo()
{
  echo foo
  sleep 1
}
( time foo ) 2>&1 | awk '/real/{print "Took "$2}'

Open in new window

0
 
troyswildridePresidentCommented:
The assisted solution does a nice job of timing the function but it does not display output to the screen.

OUTPUT:
$ './test.sh'
Took 0m1.007s

EXPECTED OUTPUT:
$ './test.sh'
foo
Took 0m1.007s

Why does the echo not display on the screen?
If that can work this is exactly what I'm looking for.

cheers.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now