in reply to tr faster than eq?

When the code is evalled, your lexical $string is not in scope, so all these tests are running against undef.

If I change the declaration to our $string, and refer to $::string within all the evals I get:

Rate tr_loop eq_loop tr eq tr_loop 96844/s -- -26% -96% -98% eq_loop 130666/s 35% -- -95% -97% tr 2436420/s 2416% 1765% -- -41% eq 4137436/s 4172% 3066% 70% --
which seems more reasonable.

Hugo

Replies are listed 'Best First'.
Re^2: tr faster than eq?
by Keystroke (Scribe) on Mar 10, 2005 at 18:37 UTC
    That explains my benchmark code results. But I still have
    conflicting results from -Devel::SmallProf for execution
    times between tr and eq.
    count wall tm cpu time line 16183 0.197199 0.700000 368: next unless ($_ = ($_ =~ tr/1/1/)); count wall tm cpu time line 16183 0.282621 0.750000 369: next if ($_ eq '0000000000000'); 15923 0.157497 0.890000 370: $_ = ($_ =~ tr/1/1/);
Re^2: tr faster than eq?
by Anonymous Monk on Mar 11, 2005 at 09:15 UTC
    Changing the 'my' to 'our' is enough to do the trick. Changing '$string' to '$::string' isn't necessary; the Benchmark module runs the code in the package of the caller.