Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I had a particular need for a fast mktime. Search foo lacking, I wrote a naive implementation (below) which seems to work very well, for me at least.

The use case is parsing logfile timestamps from ctime()-ish format into time_t epoch integer. The simplifying assumption is that once you've used a smart date parser to parse one timestamp, the next one you parse can be done much faster by looking only at the previous $time result and the new H:M:S, as long as both stamps lie within the same day. Fortunately, giant logfiles are often conveniently arranged in order so that this is usually true.

The hypothesis appears confirmed by some benchmarking of 1e7 (sequential!) dates:

# POSIX::mktime took: 139 wallclock secs (33.27 usr + + 40.64 sys = 73.91 CPU) # Time::Local::timegm_nocheck took: 58 wallclock secs (57.67 usr + + 0.15 sys = 57.82 CPU) # FastMktime took: 22 wallclock secs (22.53 usr + + 0.02 sys = 22.55 CPU)
Any thoughts appreciated. Should this be cpanned, or round-canned?
#!/usr/bin/perl # Drop-in POSIX::mktime() replacement. This is optimized for repeated # calls within the same date by caching previous result: if the cache # applies, it just performs the H:M:S calculation within the same day, # if not it falls through to POSIX::mktime() and stores the result for # next invocation. use strict; use warnings; use POSIX qw(mktime); package FastMktime; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(caching_mktime); my $mk_day = 0; my $mk_mon = 0; my $mk_year = 0; my $mk_time = 0; my $mk_hms = 0; sub caching_mktime { my ($s, $m, $h, $day, $mon, $year) = @_; my $time; my $hmsarg = 3600 * $h + 60 * $m + $s; if ($mk_day > 0 && $mk_day == $day && $mk_mon == $mon && $mk_year == + $year) { $time = $mk_time + $hmsarg - $mk_hms; } else { $time = POSIX::mktime($s, $m, $h, $day, $mon, $year); $mk_day = $day; $mk_mon = $mon; $mk_year = $year; $mk_time = $time; $mk_hms = $hmsarg; } return $time; } 1;

In reply to Special case mktime: semi-sequential access by not_japh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (9)
As of 2024-04-23 18:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found