#!/usr/bin/perl use strict; use Benchmark; use File::Find; $|++; my $Usage = "$0 some/path\n"; die $Usage unless @ARGV and -d $ARGV[0]; timethese( 30, { "\n Shell find pipe" => \&try_pipe, "\n _ Opendir recursion" => \&try_opendir, "\n __ File::Find module" => \&try_filefind, }); print "\n"; sub try_pipe { open( my $find, '-|', 'find', $ARGV[0], '-type', 'f' ); open( my $conf, '>', '/tmp/bm-conf.sh-find' ); my $counter; while (<$find>) { print $conf $_; $counter++; } print " $counter\r"; } sub try_opendir { open( my $conf, '>', '/tmp/bm-conf.opendir' ); my $counter = path_recurse( $ARGV[0], $conf ); print " $counter\r"; } sub path_recurse { my ( $path, $conf ) = @_; my $nfiles; opendir( my $dir, $path ) or return; while ( my $file = readdir( $dir )) { next if ( $file =~ /^\.{1,2}$/ or -l "$path/$file" ); if ( -f _ ) { print $conf "$path/$file\n"; $nfiles++; } elsif ( -d _ ) { $nfiles += path_recurse( "$path/$file", $conf ); } } return $nfiles; } sub try_filefind { my $counter; open( my $conf, '>', '/tmp/bm-conf.filefind' ); find( sub { if ( ! -l and -f _ ) { $counter++; print $conf "$File::Find::name\n" } }, $ARGV[0] ); print " $counter\r"; }