Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^5: Should multiplication by integer be avoided in favour of division for performance reasons? (benchmark pitfalls)

by vr (Curate)
on Nov 29, 2019 at 16:24 UTC ( [id://11109441]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Should multiplication by integer be avoided in favour of division for performance reasons? (benchmark pitfalls)
in thread Should multiplication by integer be avoided in favour of division for performance reasons?

Thank you for looking into this, dave_the_m. But there's still performance issue if integer, or float that appears to be integer, is not the first, but last element of array. Then there's just one expensive-to-free element in derived array?

use strict; use warnings; use Devel::Peek; use Benchmark qw/ cmpthese timeit /; our @a = map log, 2..1e6; push @a, 0/1; print "************ a ************\n"; Dump $a[-2]; Dump $a[-1]; my @b = map $_ * 4, @a; print "************ b ************\n"; Dump $b[-2]; Dump $b[-1]; cmpthese( -2, { 1 => 'my @c = map $_ * 4, @a', 2 => 'my @c = map $_ / (1/4), @a', }); __END__ ************ a ************ SV = NV(0x55f0580) at 0x55f0598 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 13.8155105579643 SV = NV(0x102a388) at 0x102a3a0 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 0 ************ b ************ SV = NV(0xae879c8) at 0xae879e0 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 55.2620422318571 SV = PVNV(0x714ec8) at 0xae879f8 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 0 NV = 0 PV = 0 Rate 1 2 1 10.3/s -- -20% 2 12.8/s 25% --
  • Comment on Re^5: Should multiplication by integer be avoided in favour of division for performance reasons? (benchmark pitfalls)
  • Download Code

Replies are listed 'Best First'.
Re^6: Should multiplication by integer be avoided in favour of division for performance reasons? (benchmark pitfalls)
by dave_the_m (Monsignor) on Nov 29, 2019 at 19:32 UTC
    Bear in mind that Benchmark.pm does something like the equivalent of
    eval q[ for my $i (1..$n) { my @c = map $_ * 4, @a; } ]
    Since the same multiply op is used for all iterations, once it gets "poisoned" by trying to return an IV value for the last element of the first iteration, further calls to that op will start returning PVNVs. So for iterations 2+, all elements of @c will be PVNVs and thus slower to free.

    It's also the case that multiply is fractionally slower at returning a PVNV value rather than an NV value.

    Dave.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-25 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found