use strict; use warnings; use List::Util 'reduce'; use Benchmark qw(cmpthese); our $path = shift || '/usr/bin/*'; sub badsort { my @list = sort { (stat $b)[9] <=> (stat $a)[9] } glob "$path"; my $newest = $list[0]; } sub goodsort { my %file_date; for ( glob "$path" ) { $file_date{$_} = (stat)[9]; } my $newest = ( sort { $file_date{$b} <=> $file_date{$a} } keys %file_date )[0]; } sub badreduce { my $newest = reduce { (stat $a)[9] < (stat $b)[9] ? $b : $a } glob "$path"; } sub goodreduce { my $newest = ( reduce { $a->[1] < $b->[1] ? $b : $a } map { [ $_, (stat)[9] ] } glob "$path" )->[0]; } sub d { my %file_date; my $max = -99999999; # set it to first file's mtime would be better # but just for demonstration here my $mtime; my $file; for ( glob "$path" ) { $mtime = (stat)[9]; if ($max <= $mtime) { $file = $_; $max = $mtime; } } my $newest = $file; } sub e { my @array = `ls -lrt $path`; my $newest = $array[-1]; } cmpthese(250, { zaxo_first => \&goodreduce, graff_hash => \&goodsort, more_stats => \&badreduce, most_stats => \&badsort, sk_maxfile => \&d, the_ls_lrt => \&e, });