http://www.perlmonks.org?node_id=280587


in reply to Re: Re: Adventures in optimization (or: the good and bad side of being truly bored)
in thread Adventures in optimization (or: the good and bad side of being truly bored)

on further investigation... 10s of thousands more .. calls

Are you sure? I thought the cache worked like this

$cache{$yearmonth}+$days*(24*60*60)+$hours*(60*60)+$mins*60+$secs;

Having said that, I'm in the odd position that I too didn't realize the nocheck option in Time::Local, and I also wrote my own caching for it, but I did it based on hour. I am parsing dates like "20030401101123" and i endup doing something like the following (time_to_unix is just a wrapper around timelocal that knows how to split the above string (fragment) correctly)

($cache{substr($date,0,10)}||=time_to_unix(substr($date,0,10))) + substr($date,10,2)*60 + substr($date,12,2);

which also gave me a several thousandfold time increase in my time calculations. Incidentally I think this approach will probably signifigantly outperform using timelocal() (and its caching) directly. The hash lookup on the first section of the date is far cheaper than splitting the date and then passing all of its parts on the stack, having timelocal do its checks and caching, which presumably resemble

$cache{$year.$month}

anyway, and then getting the results back over the stack. We trade many ops for just a few. And we get a cool bonus, since Time::Local is still validating its input the cache actually acts as a validating filter too. Only valid YMDH's get into it, and if we dont have a hit we either have an unknown valid YMDH or a bad date. Both of which Time::Local handles for us. So we get a serious speed benefit without losing any of the safety of Time::Local.


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...