Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Any reason NV is not marked as POK when accessed as string? (updated)

by haukex (Archbishop)
on Mar 23, 2017 at 12:11 UTC ( [id://1185620]=note: print w/replies, xml ) Need Help??


in reply to Any reason NV is not marked as POK when accessed as string?

You could also force stringification to get an even bigger speedup compared to calling pack on every hash access (if that's what you're doing), e.g. $x = "$x" or $_="$_" for @values;. Update: Added the pre-packed test with $p, which surprisingly is still not as fast as the stringified float $y. Update 2: By giving each test case its own hash instead of using one hash for all tests, now the test cases "y" and "p" perform roughly the same, as I would have originally expected (Update 3: However, across multiple runs of this benchmark, "y" still tends to outperform "p", often significantly). Update 4: If you set $x = 42.123456789, then "p" consistently outperforms "y", so I'd guess it has to do with the hash key length. (last update, I promise ;-) )

use Benchmark qw/ cmpthese /; my $x = 42.0; my $y = "$x"; my $p = pack 'F', $x; my (%h1,%h2,%h3,%h4); cmpthese( -1, { F => sub { $h1{ pack 'F', $x } = 1 }, s => sub { $h2{ $x } = 1 }, y => sub { $h3{ $y } = 1 }, p => sub { $h4{ $p } = 1 }, }); __END__ Rate s F p y s 4032984/s -- -64% -84% -84% F 11327209/s 181% -- -54% -56% p 24707825/s 513% 118% -- -5% y 25997751/s 545% 130% 5% --

Replies are listed 'Best First'.
Re^2: Any reason NV is not marked as POK when accessed as string? (updated)
by vr (Curate) on Mar 23, 2017 at 13:14 UTC

    Maybe I'll int the whole structure from orbit since they are integer pixels anyway.

    BTW, it's curios, too: int not only returns value, but marks its argument variable as 'IOK' if it 'looks like integer', even when called in void context. Therefore subsequent access as string caches PV. Clever Perl.

    C:\>perl -MDevel::Peek -e "$x=42.0; int $x; qq/$x/; Dump $x" SV = PVNV(0x38bb4) at 0xd2227c REFCNT = 1 FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK) IV = 42 NV = 42 PV = 0xd162ac "42"\0 CUR = 2 LEN = 10

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1185620]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found