Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have timestamp log that looks like this:

2000-11-09 12:19:08.000 2000-11-09 14:09:17.000 2000-11-09 15:23:55.000 2000-11-09 13:12:23.000

I want to get the difference between the earliest and the latest timestamp

This is what I currently have, but I suspect that it can be made MUCH more efficient

What do you think?

use Time::Local; # Array read from log; contains timestamps in any order: my @TIMES = <LOG>; # Sort array to get the smallest timestamp first and the largest t +imestamp last: sort(@TIMES); # re-arrange order to use in timelocal function: my ($year1,$mon1,$mday1,$hours1,$min1,$sec1) = ( $TIMES[0] = +~ /(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+).000/ ); my ($year2,$mon2,$mday2,$hours2,$min2,$sec2) = ( $TIMES[$#TIMES] = +~ /(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+).000/ ); # get timestamp in seconds since beggining of time (in unix world) +: $START_LAUNCH = timelocal($sec1,$min1,$hours1,$mday1,$mon1,$year1) +; $END_LAUNCH = timelocal($sec2,$min2,$hours2,$mday2,$mon2,$year2) +; # Get difference & Round off to two decimals: $TimeInMinutes = sprintf("%.2f",(($END_LAUNCH - $START_LAUNCH)/60) +); return $TimeInMinutes;

Replies are listed 'Best First'.
Re: Efficiently calculate the difference between TimeStamps in LOG
by extremely (Priest) on Nov 10, 2000 at 06:40 UTC
    Well in looking at it, I'd say your time calcs are pretty good. Really throwing modules at that will only slow you down.

    OTOH I'd probably not slurp the whole file just for this.

    my ($min,$max); $min=$max=<LOG>; while (my $ts=<LOG>) { $min = ($min gt $ts) ? $ts : $min; $max = ($max lt $ts) ? $ts : $max; }

    You can use the string order sorting to your advantage with that timestamp and not load the whole file into memory. Handy trick...

    --
    $you = new YOU;
    honk() if $you->love(perl)