in reply to Re: Date Manipulation Calculation Question - performance alternative
in thread Date Manipulation Calculation Question
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 ) ) ); } }
|
|---|