Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Performance issue in the loop.

by bliako (Monsignor)
on Feb 23, 2022 at 09:36 UTC ( [id://11141578]=note: print w/replies, xml ) Need Help??


in reply to Performance issue in the loop.

...and my code started to work ~30 percent faster.

my guess is that it is because you have eliminated 1 out of 4 array accesses. But then with all sorts of caching with modern CPUs and OS, it's quite spectacular that it works as in the textbook case (1/4=25%) and even better! Perhaps I miss something ...

How about using a temp variable, declared outside of the scope, to cache $sums[ $j - $i ], like: ( $tmp = $sums[ $j - $i ] += $_[ $j ] ) > $max and $max = $tmp; Does it make a difference?

Any ideas why every ~10th run any of these programs runs much slower?
push @max, $max;

this has to extend @max from time to time. And since all data is random, its size is unpredictable. But this can not explain the regularity of performance decrease (re: > 1.5x every 10th time). On the other hand, the interaction of two or more uniform distributions create normal-distribution effects (re: Central Limit Theorem).

The least you can do is to print the size of @max at the end of each program run and see if there is some correlation with running times?

If your algorithm allows it, you may want to switch the loops order. First $j and then $i so that $_[ $j ]; is cached in the outer loop and you eliminate a lot of array access.

Regarding timing your program, I use this:

use Time::HiRes qw/time/; my $start_time = Time::HiRes::time(); ... my $time_taken = Time::HiRes::time() - $start_time; # floating seconds

bw, bliako

Log In?
Username:
Password:

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

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

    No recent polls found