Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
to be faster than... if (-e $path) { if (-l $path) { print "is a link"; } elsif (-d _) { print "is a direcotry \n"; } elsif (-f _) { print "Is a file"; } else { print "Error"; } }
as the method1 does one less system call (at least) I expect method1 to be faster.if (-e $path) { if (-l $path) { print "is a link"; } elsif (-d $path) { print "is a direcotry \n"; } elsif (-f $path) { print "Is a file"; } else { print "Error"; } }
How is that reuse of file stat data slower ?#!/opt/perl_5.8.7/bin/perl use Benchmark qw(:all); my $path = "test.lnk"; my $coderef = sub { if (-e $path) { if (-l $path){ print "path is link \n"; } elsif (-d _) { print "Path is directory \n"; } elsif (-f _) { print "Path is file \n"; } } }; my $coderef1 = sub { if (-e $path) { if (-l $path){ print "path is link \n"; } elsif (-d $path) { print "Path is directory \n"; } elsif (-f $path) { print "Path is file \n"; } } }; my $r = cmpthese( 50000000, { statreuse => $coderef->(), nostatreuse => $coderef1->() });
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Use of undescore to reuse file stat data is slower
by Fletch (Bishop) on Jul 10, 2006 at 14:44 UTC | |
by Anonymous Monk on Jul 11, 2006 at 03:34 UTC | |
|
Re: Use of undescore to reuse file stat data is slower
by demerphq (Chancellor) on Jul 10, 2006 at 15:46 UTC | |
|
Re: Use of undescore to reuse file stat data is slower
by eric256 (Parson) on Jul 10, 2006 at 17:41 UTC | |
|
Re: Use of undescore to reuse file stat data is slower
by swampyankee (Parson) on Jul 10, 2006 at 15:00 UTC |