Operating System - HP-UX
1828792 Members
2415 Online
109985 Solutions
New Discussion

Elapsed time in a script?

 
SOLVED
Go to solution
Kris Spander
Advisor

Elapsed time in a script?

Hello,

I am trying to put some benchmarking data in my scripts. I would like to know if there is a way to easily calculate the amount of time that has passed. I can't just use date because some commands may run overnite. Any ideas?

Thank you,
Kris
Dilbert is my hero.
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Elapsed time in a script?

I'm sure others are going to suggest the timex command. You can man timex, if you like. Here's my dumb and simple method:

TIME_0=$(perl -e 'print time')
cmd1
cmd2
cmd3
TIME_NOW=$(perl -e 'print time')

SECONDS=$((${TIME_NOW} - ${TIME_0}))
echo "Elapsed Time: ${SECONDS} seconds"

Regards, Clay
If it ain't broke, I can fix that.
Kris Spander
Advisor

Re: Elapsed time in a script?

Thanks very much!! I had already used timex and it was not what I was looking for. I'm beginning to think that learning Perl is a must.

Thank you,
Kris
Dilbert is my hero.
john korterman
Honored Contributor

Re: Elapsed time in a script?

Hi Kris,

it is a bit ironically to be so late on a time question, but have you tried the ksh variable SECONDS?
For example:

#!/bin/ksh
#
# Show time!
#
TIME1=$SECONDS
sleep 5
TIME2=$SECONDS
echo $TIME1
echo $TIME2

it is described in the man page for ksh.

regards John
it would be nice if you always got a second chance