[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2008-06-13
10
Medium Priority
?
1,885 Views
Last Modified: 2013-11-15
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
Comment
Question by:tate1615
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 21781456
you may use the command time for this

time scriptname
0
 

Author Comment

by:tate1615
ID: 21781495
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
 
LVL 84

Expert Comment

by:ozo
ID: 21782431
in a tcsh script, you could
set time=0
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 48

Accepted Solution

by:
Tintin earned 700 total points
ID: 21782442
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
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21784015
you can use time in your script.

foo()
{
  echo foo
  sleep 2
}
time foo

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 21784042
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
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21784201
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
 
LVL 48

Expert Comment

by:Tintin
ID: 21784439
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
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 700 total points
ID: 21784551

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

Open in new window

0
 

Expert Comment

by:troyswildride
ID: 22966224
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question