trs80 has asked for the wisdom of the Perl Monks concerning the following question:

This was from another node, but I would like to post it as the main question. The original question was anwsered, but when benchmarking it I noticed a large difference in the speed of the operation, myself and the monk who posted the orignal question are curious as to why. Here is the code:
#!perl use strict; use File::Basename; use CGI; use Benchmark; use Win32; my $cgi = CGI->new(); print $cgi->header; print "Hello World<br>"; print "DOS NAME: " . basename($0) . "<BR>"; print "ENV BASENAME: " . basename($ENV{SCRIPT_FILENAME}) . "<br>"; print "Win32 BASENAME: " . basename(Win32::GetLongPathName($0)) . "<br +>"; timethese (100000, { 'basename_env' => sub { basename(Win32::GetLongPathName($0)) +}, 'basename_win32' => sub { basename($ENV{SCRIPT_FILENAME}) }, 'basemane_dollar0' => sub { basename($0) }, } ); 1;
        The results 

Benchmark: timing 100000 iterations of 
    basemane_dollar0, 
    basename_env, 
    basename_win32...
basemane_dollar0:  5 wallclock secs 
    ( 3.68 usr +  0.00 sys =  3.68 CPU) @ 27203.48/s (n=100000)
basename_env: 121 wallclock secs 
    (34.99 usr + 81.07 sys = 116.06 CPU) @ 861.65/s (n=100000)
basename_win32:  5 wallclock secs 
    ( 4.24 usr +  0.00 sys =  4.24 CPU) @ 23557.13/s (n=100000)
Now I would like to know why the $ENV way is SO slow. I had expected there might be a little difference, but 10 times slower? At first I thought is was partly the hash lookup, but even when I did a sub with the string (path) instead of the hash it was still slow.

Replies are listed 'Best First'.
Re: Slow filename lookup on Win32
by Kanji (Parson) on Jan 23, 2002 at 08:34 UTC

    If that's your actual benchmark code, you've mixed up the names of the env and win32 tests, so it's actually Win32::GetLongPathName($0) that's taking the longest.

        --k.


      DOH! Then I rephrase my question:
      Why is the Win32::GetLongPathName so much slower then?
      Does it create a new object every time?
Re: Slow filename lookup on Win32
by Anonymous Monk on Jan 23, 2002 at 08:51 UTC
    Win32::GetLongPathName($0) is slower because it is making a native Win32 api call (that's the short answer, I don't know the long one).