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;

In reply to Efficiently calculate the difference between TimeStamps in LOG by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.