Perl: the Markov chain saw PerlMonks

Comment on

 Need Help??

Another small speed up can be achieved by replacing

```my \$leaps =  int( (\$y - 1970) / 4 + 0.5 );
((((\$y-1970)*365 +\$leaps+MONTHS->{\$m}+(\$d-1))*24 +\$H)*60 +\$M)*60 +
+\$S;

with

```(((int((\$y-1970)*365.25-.5)+MONTHS->{\$m}+\$d)*24 +\$H)*60 +\$M)*60 +\$S;

in my experiments between 5 to 10%. All other attempts using split and unpack are much slower. I found substr to be very fast:

```sub str2epoch3 {
(((int((substr(\$_[0],12,4)-1970)*365.25-.5)+
MONTHS->{substr(\$_[0],8,3)}+substr(\$_[0],5,2))*24 +substr(\$_[0],1
+7,2))*60 +
substr(\$_[0],20,2))*60 +substr(\$_[0],23,2);
}

about 60% faster than BrowserUk's code. I wonder whether there is something wrong...

Here is my full code:
```use strict;
use warnings;
use Benchmark qw/cmpthese/;

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
] };

sub str2epoch {
my( \$d, \$m, \$y, \$H, \$M, \$S ) = \$_[0] =~
m[^.... (\d\d) (...) (\d\d\d\d) (\d\d):(\d\d):(\d\d)]
my \$leaps =  int( (\$y - 1970) / 4 + 0.5 );
((((\$y-1970)*365 +\$leaps+MONTHS->{\$m}+(\$d-1))*24 +\$H)*60 +\$M)*60 +
+\$S;
}

sub str2epoch2 {
my( \$d, \$m, \$y, \$H, \$M, \$S ) = \$_[0] =~
m[^.... (\d\d) (...) (\d\d\d\d) (\d\d):(\d\d):(\d\d)]
(((int((\$y-1970)*365.25-.5)+MONTHS->{\$m}+\$d)*24 +\$H)*60 +\$M)*60 +\$
+S;
}

sub str2epoch3 {
(((int((substr(\$_[0],12,4)-1970)*365.25-.5)+
MONTHS->{substr(\$_[0],8,3)}+substr(\$_[0],5,2))*24 +substr(\$_[0]
+,17,2))*60 +
substr(\$_[0],20,2))*60 +substr(\$_[0],23,2);
}

my \$str = 'Fri, 01 Mar 2013 01:21:14 +0000';

cmpthese( -3, {BrowserUK => sub { str2epoch (\$str) },
BUK2      => sub { str2epoch2(\$str) },
substr    => sub { str2epoch3(\$str) }, }
);

In reply to Re^2: High-speed Date Formatting by hdb
in thread *SOLVED* High-speed Date Formatting by Endless

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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the monastery is silent...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2018-03-17 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (224 votes). Check out past polls.

Notices?