Hi,
The perldoc says :
If any of the file tests (or either the "stat" or "lstat" operators) are given the special filehandle consisting of a solitary underline, then the stat structure of the previous file test (or stat operator) is used, saving a system call.
So I would expect that the code chunk
Method 1
---------
... 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"; } }
to be faster than
Method 2
---------
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"; } }
as the method1 does one less system call (at least) I expect method1 to be faster.
But when I benchmarked the results tell other wise :
%./reg.pl
Path is file
Path is file
Rate statreuse nostatreuse
statreuse 31847134/s -- -39%
nostatreuse 52083333/s 64% --
%cat reg.pl
#!/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->() });
How is that reuse of file stat data slower ?
Do I miss anything here ?
thanks,
sateesh

In reply to Use of undescore to reuse file stat data is slower by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.