#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); use POSIX 'strftime'; my @months=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); sub getMonthNumber { return int(rand(12)); } sub preBuiltArrayLookup { return $months[getMonthNumber()]; } sub inlineArrayLookup { return qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[getMonthNumber()]; } sub substrLookup { return substr('JanFebMarAprMayJunJulAugSepOctNovDec',getMonthNumber()*3,3); } # so as to not penalize strftime with localtime call my @fixedLocaltime=localtime; sub strftimeLookup { return strftime "%m",@fixedLocaltime; } cmpthese(-2, { 'preBuiltArrayLookup' => \&preBuiltArrayLookup, 'inlineArrayLookup' => \&inlineArrayLookup, 'substrLookup' => \&substrLookup, 'strftimeLookup' => \&strftimeLookup, } );