Converting a string such as 20041101174146 to a number of seconds would take at least 6 mathematical operations for the 2004, 11, 01, 17, 41, and 46 components.

Well, one thing to recall here is that if the operation occurs regularly then caching the results will result in a signifigant speedup. For instance you could cache the year/month/day/hour results and then add in only the minutes and seconds. This means that a lot of the operations you mention here wouldn't have to happen unless the cache was empty and even then would only happen once.

After a LOT of benchmarking I found the following routine works best for my situation (parsing millions of datestamps a day), obviously other usage cases may have different performance effects.

{ my (%hourcache,%mincache); sub D14_to_unix { my $hour = substr( $_[0], 0, 10 ); my $min = substr( $_[0], 10, 4 ); return +( $hourcache{$hour} ||= timelocal( 00, 00, substr( $hour, 8, 2 ), substr( $hour, 6, 2 ), substr( $hour, 4, 2 ) - 1, substr( $hour, 0, 4 ) - 1900 ) ) + ( $mincache{$min} ||= ( substr( $min, 0, 2 ) * 60 + substr( $min, 2, 2 ) ) ); } }
---
demerphq


In reply to Re^2: Date Manipulation Calculation Question - performance alternative by demerphq
in thread Date Manipulation Calculation Question by EchoAngel

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.