Created
June 20, 2013 09:35
-
-
Save pmakholm/5821490 to your computer and use it in GitHub Desktop.
Benchmark of listing files in directory in perl: ls vs glob vs readdir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Benchmark qw(cmpthese); | |
qx(ls -1U); # Populate dir cache | |
cmpthese(1000, { | |
'ls-1 (readline)' => sub { open my $fh, "-|", "ls -1"; my @result = <$fh>; }, | |
'ls-1U (readline)' => sub { open my $fh, "-|", "ls -1U"; my @result = <$fh>; }, | |
'ls-1 (split)' => sub { my $result = qx(ls -1); my @result = split /\n/, $result; }, | |
'ls-1U (split)' => sub { my $result = qx(ls -1U); my @result = split /\n/, $result; }, | |
'glob' => sub { my @result = glob("*"); }, | |
'readdir' => sub { opendir(my $dh, "."); my @result = readdir($dh); }, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: