Hello my new favorite friends,
As part of a lexical processing project I'm working on, I'm parsing millions of dates and converting them to Epoch time. However, Diag::NYTProf showed me that I was losing massive amounts of time by using use Date::Parse::str2time; I guess that's the price you pay for something that seemed like the perfect, effortless way to parse the dates.
So, my question is, how can I most efficiently parse these dates, for those of you who have a sense of the benchmarks? Here was the WRONG way (removing it doubled my speed!):
# Dates of form 'Fri, 01 Mar 2013 01:21:14 +0000'
my $created_at = str2time($value);
Update: Solution
Thanks to the discussion between BrowserUK and rjt I high-speed solution came that looked something like this:
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";
my $newDate = epoch_sec($date);
say $newDate;
Thanks! You guys are incredible.
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, 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?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|