This is mainly in answer to the comment to my previous post on revisiting
plog.
plog is a piece of code ready for copy+paste into whatever codebase you're in at the moment, and almost completely agnostic. Yes, in code where I'm already using DateTime, then I use those routines.
You could also create a small sub to hide the "nasty idiom".
Or you could use Date::Format instead (ca. 10 kB codebase instead of 110 kB, and between one and two orders of magnitude quicker for this particular purpose):
require Date::Format;
@lt = localtime();
$dt = strftime("%Y-%m-%d %T",\@lt);
Of course, that leaves you with the problem of whether the TimeDate packages are installed or not, and another test for that.
Besides, the bloat is not exactly insignificant if you have code that's running repeatedely.
The following example with 100,000 repetitions may seem ludicrous, but I actually have production code where timestamped log entries run into that order of magnitude. And yes, I would very much like to save that extra time.
Edit 2009-06-09 00:32 UTC: thanks to Dave Rolsky for the simplified testing code, and to Ilmari for reminding me of POSIX::strftime, which I'd previously rejected based on other people's claims that it was dog slow. I've removed the home-grown tests in favour of the results from
a slightly modified version of Dave's sample test script.
Here are the numbers for 100k iterations, times derived from the rates:
| Solution | Time | Rate |
|---|
| DateTime | 490.2 s | 204/s |
| DateTime (cached tz) | 76.7 s | 1 304/s |
| Date::Format | 6.4 s | 15 528/s |
| POSIX | 3.3 s | 30 030/s |
| localtime | 0.6 s | 163 934/s |