in reply to Re^4: A Scalar::Util::refaddr Oddity
in thread A Scalar::Util::refaddr Oddity

If it's backwards compatible, and if speed is the utility function, yes. Surprisingly, unless I'm missing something, it's even faster than refaddr, at least for my environment (perl 5.8.6 on linux i386).

Update: clearly, what I'm missing is reading the docs to Benchmark and proofreading my code. Stupid errors fixed, I hope. 0 + $ref still seems to be faster than the XS refaddr, which is surprising, but maybe that's the function call overhead.

use strict; use warnings; use Scalar::Util qw( refaddr ); use Benchmark qw( cmpthese ); my $ref = {}; cmpthese( -5, { 'xs ' => sub { refaddr $ref }, 'regex' => sub { "$ref" =~ /0x(\w+)/; hex $1; }, '0+ref' => sub { 0 + $ref }, });

Result:

Rate regex xs 0+ref regex 165816/s -- -89% -97% xs 1504549/s 807% -- -73% 0+ref 5550057/s 3247% 269% --

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.