perl, string compare, dprof
This may be old news for people more familiar with perl internals.
dpk@dpk1:~$ perl -version | head -2
This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
dpk@dpk1:~$ cat test1.pl
#!/usr/bin/perl
use strict;
my $x = 0;
while ($x < 100)
{
&doit();
$x++;
}
sub doit() {
my $foo = 'foobar'; my $x = 0; my $y = 0;
while ($x < 1000000)
{
if ($foo eq 'foofoo')
{
$y++;
}
$x++;
}
}
dpk@dpk1:~$ perl -d:DProf test1.pl;dprofpp
Total Elapsed Time = 52.07938 Seconds
User+System Time = 52.07938 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
99.9 52.07 52.070 100 0.5207 0.5207 main::doit
0.00 - -0.000 1 - - strict::bits
0.00 - -0.000 1 - - strict::import
0.00 - -0.000 1 - - main::BEGIN
And then:
dpk@dpk1:~$ cat test2.pl
#!/usr/bin/perl
use strict;
my $x = 0;
while ($x < 100)
{
&doit();
$x++;
}
sub doit() {
my $foo = 'foobar'; my $x = 0; my $y = 0;
while ($x < 1000000)
{
if ('foofoo' eq $foo)
{
$y++;
}
$x++;
}
}
Total Elapsed Time = 51.31948 Seconds
User+System Time = 51.30948 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
100. 51.31 51.310 100 0.5131 0.5131 main::doit
0.00 - -0.000 1 - - strict::bits
0.00 - -0.000 1 - - strict::import
0.00 - -0.000 1 - - main::BEGIN
I’ve repeated this little test multiple times, with different several different inputs, but the result is always the same. if ('string' eq $variable) is faster than if ($variable eq 'string')