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.


In reply to Re^5: A Scalar::Util::refaddr Oddity by xdg
in thread A Scalar::Util::refaddr Oddity by Zaxo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.