use Benchmark qw(cmpthese countit); use POSIX qw(strftime); use constant TIME => 10000000; use constant FORMAT => '%Y-%m-%d %H:%M'; my $trash; sub plain_strftime { return strftime(FORMAT, localtime(shift)); } my ($timecache, $strcache) = (0, &plain_strftime($timecache)); sub cache_strftime { my $t = shift; if ($t - $timecache > 60) { $timecache = $t; $strcache = &plain_strftime($timecache); } return $strcache; } cmpthese(TIME, { 'plain' => sub { $trash = &plain_strftime(time); }, 'cache' => sub { $trash = &cache_strftime(time); }, }); __DATA__ laptop:~> monk.pl Benchmark: timing 10000000 iterations of cache, plain... cache: 79 wallclock secs (67.66 usr + 3.69 sys = 71.35 CPU) @ 140154.17/s (n=10000000) plain: 282 wallclock secs (248.43 usr + 7.03 sys = 255.46 CPU) @ 39145.07/s (n=10000000) Rate plain cache plain 39145/s -- -72% cache 140154/s 258% --