Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: High-speed Date Formatting

by rjt (Deacon)
on Jul 12, 2013 at 00:39 UTC ( #1043844=note: print w/ replies, xml ) Need Help??


in reply to *SOLVED* High-speed Date Formatting

Update: Another quantum leap brought to you by Inline::C and <time.h> for a 2500% increase over Date::Parse::str2time. Fast enough, I trust?

Updated with BrowserUk's method

This one's a natural for split (prior to getting really funky)

Benchmark code

#!/usr/bin/env perl use 5.012; use warnings; use Benchmark qw/cmpthese/; use Date::Parse; # str2time use Time::Local qw/timegm_nocheck/; my %mon; @mon{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = 0..11; # For BrowserUk's sub use constant MONTHS => { qw[ Jan 0 Feb 31 Mar 59 Apr 90 May 120 Jun 151 Jul 181 Aug 212 Sep 242 Oct 272 Nov 303 Dec 334 ] }; use Inline C => q@ int epoch_sec(char * date) { char *tz_str = date + 26; struct tm tm; int tz; if ( strlen(date) != 31 || strptime(date, "%a, %d %b %Y %T", &tm) == NULL || sscanf(tz_str, "%d", &tz) != 1) { printf("Invalid date %s\n", date); return 0; } return timegm(&tm) - (tz < 0 ? -1 : 1)*(abs(tz)/100*3600 + abs(tz)%100*60); } @; our $date = "Fri, 01 Mar 2013 01:21:14 +0200"; cmpthese(-3, { str2time => sub { str2time($date) }, split_nocheck => sub { my (undef, $dd, $Mm, $yyyy, $hh, $mm, $ss, $tz) = split /[ ,:]+/o, $date; timegm_nocheck($ss, $mm, $hh, $dd, $mon{$Mm}, $yyyy) - ($tz < 0 ? -1 : 1)*(substr($tz,1,2)*3600 + substr($tz,3) +*60) }, BrowserUk => sub { my( $d, $m, $y, $H, $M, $S, $tz ) = $date =~ m/^.... (\d\d) (...) (\d\d\d\d) (\d\d):(\d\d):(\d\d) ([+-] +\d{4})/ or die "Bad format $date"; my $leaps = int( ($y - 1970) / 4 + 0.5 ); (((($y-1970)*365 +$leaps+MONTHS->{$m}+($d-1))*24 +$H)*60 +$M)* +60 +$S - ($tz < 0 ? -1 : 1)*(substr($tz,1,2)*3600 + substr($tz,3) +*60) }, inline_c => sub { epoch_sec($date) }, });

Output:

Rate str2time split_nocheck BrowserUk +inline_c str2time 16554/s -- -87% -93% + -96% split_nocheck 126315/s 663% -- -45% + -71% BrowserUk 229820/s 1288% 82% -- + -47% inline_c 437015/s 2540% 246% 90% + --


Comment on Re: High-speed Date Formatting
Select or Download Code
Replies are listed 'Best First'.
Re^2: High-speed Date Formatting
by Endless (Beadle) on Jul 12, 2013 at 15:48 UTC

    Whoo! Looks like that's the winner rjt. I suspect the ability to switch into c-mode can be a major strength for Perl.

Re^2: High-speed Date Formatting
by erh (Initiate) on Apr 17, 2014 at 21:41 UTC
    The constants for BrowserUk are wrong. They should be:
    use constant MONTHS => { qw[ Jan 0 Feb 31 Mar 59 Apr 90 May 120 Jun 151 Jul 181 Aug 212 Sep 243 Oct 273 Nov 304 Dec 334 ] };

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043844]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2015-07-30 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls