Well I was almost ready to post a $ENV{SCRIPT_FILENAME} option,
but before I did I ran a benchmark on the three options (includes
the option of the wrong filename).
It seems that the Win32::GetLongPathName is in deed the
fastest with the correct filename. Here is my simple Benchmark 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?
UPDATE: The above code has a glaring error. The subs
for env and win32 were flip flopped in the timethese function.
The Win32 way is 10 times slower not the ENV way. I apologize
for the late night code :^) |