Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: does perl have branch prediction

by david2008 (Scribe)
on Jan 29, 2014 at 06:38 UTC ( #1072440=note: print w/ replies, xml ) Need Help??


in reply to Re: does perl have branch prediction
in thread does perl have branch prediction

Good point Here is my code

use strict; use warnings; use Benchmark; use List::Util qw /shuffle/; use feature 'say'; my $n = 10**7; my @sorted = 1..$n; my @random = shuffle @sorted; my $t0 = Benchmark->new; say sum_this(\@sorted); my $t1 = Benchmark->new; my $td1 = timediff($t1, $t0); say "sorted:",timestr($td1); my $t2 = Benchmark->new; say sum_this(\@random); my $t3 = Benchmark->new; my $td2 = timediff($t3, $t2); say "random:",timestr($td2); sub sum_this { my ($arr) = @_; my $sum; foreach (my $i=0;$i<=$#$arr;$i++) { my $x = $arr->[$i]; if($x>$n/2) { $sum+= $x; } } return $sum; }
The output is
37500002500000
sorted: 6 wallclock secs ( 5.61 usr +  0.00 sys =  5.61 CPU)
37500002500000
random: 6 wallclock secs ( 5.81 usr +  0.00 sys =  5.81 CPU)
There is a difference but not as dramatic as in java or c++ (from the stack overflow post) What is the correct interpretation of the results?


Comment on Re^2: does perl have branch prediction
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2015-07-05 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls