How can I tell how long something took?

How can I tell how long something took?

Post by Clif J. Smit » Tue, 02 Jun 1998 04:00:00



I'd like to add the logic in my backup script of exactly how long it took.
I've got a start time and an end time, how can I subtract or possibly start
a counter?  Also, I have to account for an ending time in a different day as
I start some backups around 10pm.

Any ideas are appreciated.

thanks, Clif

 
 
 

How can I tell how long something took?

Post by Glenn Wes » Tue, 02 Jun 1998 04:00:00



> I'd like to add the logic in my backup script of exactly how long it took.
> I've got a start time and an end time, how can I subtract or possibly start
> a counter?  Also, I have to account for an ending time in a different day as
> I start some backups around 10pm.

> Any ideas are appreciated.

> thanks, Clif

timex for one...

 
 
 

How can I tell how long something took?

Post by Walter Davi » Tue, 02 Jun 1998 04:00:00


man timex


Quote:>I'd like to add the logic in my backup script of exactly how long it took.
>I've got a start time and an end time, how can I subtract or possibly start
>a counter?  Also, I have to account for an ending time in a different day as
>I start some backups around 10pm.
>Any ideas are appreciated.
>thanks, Clif

 
 
 

How can I tell how long something took?

Post by Garry Garre » Tue, 02 Jun 1998 04:00:00



Quote:>I'd like to add the logic in my backup script of exactly how long it took.
>I've got a start time and an end time, how can I subtract or possibly start
>a counter?  Also, I have to account for an ending time in a different day as
>I start some backups around 10pm.

>Any ideas are appreciated.

>thanks, Clif

Why not use the command "time" which times how long a given
command (or shells script) takes?  You simply say:

time command

I think there's a way to specify how the output looks; see
the man page on "time" for more information.

--
-----------------------------------------------------------------------
Garry J. Garrett              I can't put my e-mail address in my
CSG Systems, Inc.      ._o    .signature file (without getting a ton
2525 North 117th Ave.    |>   of spam e-mail), so see my personal
Mailstop 2-A             4    web page for a "mailto:" tag to send me
Omaha, NE 68164-3679          e-mail.

CSG Systems - http://www.csgsys.com/
My Homepage - http://monarch.papillion.ne.us/~ggarrett
              http://198.247.210.200/~ggarrett (if their DNS is still broke)

 
 
 

How can I tell how long something took?

Post by Pete Sattle » Wed, 03 Jun 1998 04:00:00


Clif:

Try this:

#!/usr/bin/ksh
start=$1                       #Input STARTING time
stop=$2                        #Input ENDING time

#Get START time in seconds:
typeset -i start_hh=$(echo $start | awk '{print $1}' FS=:)
typeset -i start_mm=$(echo $start | awk '{print $2}' FS=:)
typeset -i start_ss=$(echo $start | awk '{print $3}' FS=:)
typeset -i start_secs=$(( start_ss + start_mm * 60 + start_hh * 3600 ))

#Get STOP time in seconds:
typeset -i stop_hh=$(echo $stop | awk '{print $1}' FS=:)
typeset -i stop_mm=$(echo $stop | awk '{print $2}' FS=:)
typeset -i stop_ss=$(echo $stop | awk '{print $3}' FS=:)

#Handle MIDNIGHT wrap around:
(( stop_hh < start_hh )) && let stop_hh=stop_hh+24
#Wrapped within SAME hour:
(( stop_hh == start_hh && stop_mm < start_mm )) && \
{
  let stop_hh=stop_hh+23
  let stop_mm=stop_mm+60

Quote:}

#Wrapped within SAME minute:
(( stop_hh == start_hh && stop_mm == start_mm && stop_ss < start_ss )) && \
{
  let stop_hh=stop_hh+23
  let stop_mm=stop_mm+59
  let stop_ss=stop_ss+60
Quote:}

typeset -i stop_secs=$(( stop_ss + stop_mm * 60 + stop_hh * 3600 ))

#Translate seconds back into elapsed time:
typeset -i ss_durn=$(( stop_secs - start_secs ))
typeset -i hh_durn=$(( ss_durn / 3600 ))
typeset -i ss_durn=$(( ss_durn - (hh_durn * 3600) ))
typeset -i mm_durn=$(( ss_durn / 60 ))
typeset -i ss_durn=$(( ss_durn - (mm_durn * 60) ))

#Format it and pass it back:
typeset -Z2 hh_durn
typeset -Z2 mm_durn
typeset -Z2 ss_durn
echo $hh_durn':'$mm_durn':'$ss_durn

exit 0

Pete Sattler
Chase Manhattan Bank



Quote:> I'd like to add the logic in my backup script of exactly how long it
took.
> I've got a start time and an end time, how can I subtract or possibly
start
> a counter?  Also, I have to account for an ending time in a different day
as
> I start some backups around 10pm.

> Any ideas are appreciated.

> thanks, Clif

 
 
 

How can I tell how long something took?

Post by Michael Pow » Wed, 03 Jun 1998 04:00:00


    Clif> I'd like to add the logic in my backup script of exactly how
    Clif> long it took.  I've got a start time and an end time, how
    Clif> can I subtract or possibly start a counter?  Also, I have to
    Clif> account for an ending time in a different day as I start
    Clif> some backups around 10pm.

The logic is not too difficult.  Use the 24-hour clock.  You have two
cases -- a) where the start time is smaller than the end time; and b)
where the start time is larger than the end time ( i.e., when you go
past midnight).  If you're only dealing with one case, it really
simplifies matters.

To find the total time used, convert each time to minutes from 00:00,
take the difference, and reconvert that value back to the hh:mm
format.

So, for example, 10:30 pm is 22:30 = (22*60)+30 = 1350

If you go past midnight, the trick is to first take the difference
between your two times; then subtract this value from (24*60) and you
will get the correct value.

Example:
Start: 10:30 pm = 2250
End:    2:45 am =  165

s=(22*60)+30 = 1350
e=(2*60)+45 = 165

subtotal = s-e = 1185

total=(24*60)-sub = 255

hours=total/60 = 4
minutes=total%60 = 15

mp

 ///////////////////////////////////////////////////////////////////////
 //             "Thus shines a good deed in a weary world."           //

 /////////////// pgpk -a finger://teleport.com/looie ///////////////////

 
 
 

How can I tell how long something took?

Post by Dan A. Merc » Wed, 03 Jun 1998 04:00:00



: I'd like to add the logic in my backup script of exactly how long it took.
: I've got a start time and an end time, how can I subtract or possibly start
: a counter?  Also, I have to account for an ending time in a different day as
: I start some backups around 10pm.

: Any ideas are appreciated.

: thanks, Clif

Ksh and POSIX shells have the builtin SECONDS variable.

$ SECONDS=;sleep 23;echo $SECONDS
23

By default,  it contains the number of seconds expired since the
shell started.  You can set it to any number and that is the number
it will start from:

$ SECONDS=10;sleep 3;echo $SECONDS
13

If you unset it,  it loses all special meaning.  

   SECONDS=

is equivalent to

   SECONDS=0

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

How can I tell how long something took?

Post by Ken Pizzi » Wed, 03 Jun 1998 04:00:00



Quote:>Ksh and POSIX shells have the builtin SECONDS variable.

I'm pretty certain that POSIX does not specify a SECONDS variable
for the shell.

                --Ken Pizzini

 
 
 

How can I tell how long something took?

Post by leel » Wed, 03 Jun 1998 04:00:00


If you just need to see it on screen try timex that will print real time and
processing time but as it sends directly to tty device you can't redirect to
capture to results.

If you do need to capture the time taken then you could use shell and cut
commands, i.e.

X1=`date`
HOURS1=`echo $X1|cut -c11,12`
MINUTES1=`echo $X1|cut -c14,15`
SECONDS1=`echo $X1|cut -c17,18`
INSECONDS1=`expr $HOURS1 "*" 360 "+" $MINUTES1 "*" 60 "+" $SECONDS1`
#
# Back up code here
#
X2=`date`
HOURS2=`echo $X2|cut -c11,12`
MINUTES2=`echo $X2|cut -c14,15`
SECONDS2=`echo $X2|cut -c17,18`
INSECONDS2=`expr $HOURS2 "*" 360 "+" $MINUTES2 "*" 60 "+" $SECONDS2`
if [ `echo $X1|cut -c8,9` != `echo $X2|cut -c8,9` ]
then
  echo got diff
  INSECONDS2=`expr $INSECONDS2 "+" 8640`
fi
echo $INSECONDS2
DIFFINSECONDS=`expr $INSECONDS2 "-" $INSECONDS1`
echo $DIFFINSECONDS

Not pretty but will work for processes that don't take more than 24 hours.
There may be an easier way but I can't think of it.


>I'd like to add the logic in my backup script of exactly how long it took.
>I've got a start time and an end time, how can I subtract or possibly start
>a counter?  Also, I have to account for an ending time in a different day
as
>I start some backups around 10pm.

>Any ideas are appreciated.

>thanks, Clif

 
 
 

How can I tell how long something took?

Post by Dan A. Merc » Wed, 03 Jun 1998 04:00:00



: >Ksh and POSIX shells have the builtin SECONDS variable.

: I'm pretty certain that POSIX does not specify a SECONDS variable
: for the shell.

:               --Ken Pizzini

POSIX may not be specified for the shell,  but the HP-UX "posix" shell
supports it. YMMV

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

How can I tell how long something took?

Post by Don F. Durschmi » Wed, 03 Jun 1998 04:00:00


The "time" command prints out the real, system and user time spent by a command.
In ksh the built-in time command will time a pipeline as well.  The real
time is the amount of time it took from starting the command until it
completed.  The user time is the amount of CPU time used by the code of
the process.  The system time is the amount of time the UNIX kernel spent
doing things for the process.  The output format varies from shell to
shell.

It is invoked with:

   time  ./your_executable

 
 
 

How can I tell how long something took?

Post by Jan L. Peters » Wed, 03 Jun 1998 04:00:00



> If you just need to see it on screen try timex that will print real time and
> processing time but as it sends directly to tty device you can't redirect to
> capture to results.

You should be able to use /bin/time.

time writes to stderr, but you can redirect that off to a file:

$ /bin/time yourcommand 2>timelog

if you really need to see the stderr output of yourcommand, you can
get a little more clever with redirection and sub shells:

$ /bin/time /bin/sh -c 'yourcommand 2>/dev/tty' 2>timelog

which has the drawback of actually timing the /bin/sh invocation, but
you can probably live with that overhead.  Note that many shells have
a built-in version of time that may cause you grief with redirection.
You can get around that by specifying a full path to time (plus you're
sure which one you get that way).

Note also that you must be using a bourne-style shell (sh, ksh, or
bash) in order to be able to redirect the stderr off like this.

Also, see the man page time(1) (i.e. man -s 1 time).

Hope this helps.

        -jan-
--
Jan L. Peterson         PartNET                    tel. +1 801 581 1118
Senior Systems Admin    423 Wakara Way, Suite 216  fax  +1 801 581 1785

 
 
 

How can I tell how long something took?

Post by Jan L. Peters » Wed, 03 Jun 1998 04:00:00



Quote:> I'd like to add the logic in my backup script of exactly how long it took.
> I've got a start time and an end time, how can I subtract or possibly start
> a counter?  Also, I have to account for an ending time in a different day as
> I start some backups around 10pm.

Add something like this to the start of your script:

ts_start=`perl -e 'print time, "\n";'`

and something like this at the end:

perl -e 'print "elapsed time: ", time - $ARGV[0], " seconds\n";' $ts_start

Of course, if you want a better value than just raw number of seconds,
you can add appropriate code.  

Something like this might look good:

perl -e '($sec,$min,$hour) = gmtime(time - $ARGV[0]);
         printf "elapsed time: %02d:%02d:%02d\n", $hour, $min, $sec;
        ' $ts_start

(Note that this example breaks if more than 24 hours have elapsed, you
can mod off the seconds in the current day and div off the number of
days if you need to.)

You *do* have perl, don't you?  If you don't, get it.  Learn to use it.

        -jan-
--
Jan L. Peterson         PartNET                    tel. +1 801 581 1118
Senior Systems Admin    423 Wakara Way, Suite 216  fax  +1 801 581 1785

 
 
 

How can I tell how long something took?

Post by Mvn » Thu, 04 Jun 1998 04:00:00


Many ways:

For one do:
VAR=`date +%???` before and after and then do the math to find the diff.

do man date to see how to get day of year etc from date.

Marnus van Niekerk
MJVN Computer Consulting


>I'd like to add the logic in my backup script of exactly how long it took.
>I've got a start time and an end time, how can I subtract or possibly start
>a counter?  Also, I have to account for an ending time in a different day
as
>I start some backups around 10pm.

>Any ideas are appreciated.

>thanks, Clif

 
 
 

How can I tell how long something took?

Post by brian hil » Fri, 05 Jun 1998 04:00:00



: ...
: which has the drawback of actually timing the /bin/sh invocation, but
: you can probably live with that overhead.  Note that many shells have
: a built-in version of time that may cause you grief with redirection.
: You can get around that by specifying a full path to time (plus you're
: sure which one you get that way).
: Note also that you must be using a bourne-style shell (sh, ksh, or
: bash) in order to be able to redirect the stderr off like this.

It is true that builtin "time" (the only one you _should_ be using, BTW,
since you are then not timing the overhead of "time" or the shell itself)
does indeed output to /dev/tty, as thus may not be redirected, _except_
when invoked as:

{ { time command; } 2>&1 }

Weird, but it gets the job done.

-Brian