I did a small test (Perl v5.20.1 under FreeBSD in VM) that calls the following blocks: 1,000,000 times each:
BUT
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); my $now = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
AT
my $now = strftime('%Y%m%d%H%M%S',localtime);
FROM
my $now = Time::Piece::localtime->strftime('%Y%m%d%H%M%S');
with the following results:
A: 2 seconds
B: 11 seconds
C: 19 seconds
This, of course, is not a thorough test or benchmark, but at least it reproduces for me, so although it is more complicated, I would prefer the first method if generating a datetimestamp is required very often.
Call (for example, under FreeBSD 10.1)
my $now = `date "+%Y%m%d%H%M%S" | tr -d "\n"`;
may not be such a good idea, because it is OS independent and takes a lot of time.
Regards, Holger
Holger Sep 22 '15 at 8:51 2015-09-22 08:51
source share