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

Anyway, after reblessing the object, wouldn't it be better to take the reference in numeric context than to extract the number from the string form?

Replies are listed 'Best First'.
Re^5: A Scalar::Util::refaddr Oddity
by adrianh (Chancellor) on Sep 27, 2005 at 13:08 UTC
    Anyway, after reblessing the object, wouldn't it be better to take the reference in numeric context than to extract the number from the string form?

    True, although I've no idea off the top of my head whether that behaviour has always been in Perl so there may be issues with the perl versions that Scalar::Util supports.

      You might be right... the phrase
      Using a reference as a number produces an integer representing its storage location in memory. The only useful thing to be done with this is to compare two references numerically to see whether they refer to the same location.
      has got into perlref between 5.005 and 5.6.0. The feature might have been there from the start though.
Re^5: A Scalar::Util::refaddr Oddity
by xdg (Monsignor) on Sep 27, 2005 at 15:06 UTC

    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.