You're the benchmark master. ;-) One-time hit to require overload and then a single function call, or always check blessed, store the original package and bless twice?
Or how about this -- so we only use it if something else has loaded overload and the object is actually overloaded?
sub refaddr_pp {
return if not ref $_[0];
if ( exists $INC{'overload.pm'}
&& overload::Overloaded($_[0]) ) {
overload::StrVal($_[0]) =~ /0x(\w+)/;
return hex $1;
}
else {
return 0 + $_[0];
}
}
-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.
| [reply] [d/l] |
| [reply] |
I'm trying to remember to benchmark more myself. Some quick benchmarking I did suggests they're all within the margin of error. Rest deleted
Update: Error is in front of keyboard. There's a big difference if I actually write my benchmark code correctly.
(Each loop did a refaddr on one object, one hash ref, and one ref to a constant.)
# overload used on object
Rate bless_regex strval_0+ bless_0+
bless_regex 12921/s -- -34% -37%
strval_0+ 19557/s 51% -- -5%
bless_0+ 20495/s 59% 5% --
# no overloading ( and overload not loaded )
Rate bless_regex bless_0+ strval_0+
bless_regex 13504/s -- -35% -93%
bless_0+ 20766/s 54% -- -90%
strval_0+ 206767/s 1431% 896% --
# bless_regex: fixed PP version from Scalar::Util to store package usi
+ng PP blessed, and only rebless if reference is blessed
# bless_0+: same as above, but use 0+$ref instead of regex extraction
# strval_0+: if overload loaded and reference is overloaded, use StrVa
+l to extract with regex, otherwise, use 0+$ref
-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.
| [reply] [d/l] |