Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Summing Up Array Elements By Group

by ivancho (Hermit)
on Jan 21, 2009 at 09:40 UTC ( [id://737788]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Summing Up Array Elements By Group
in thread Summing Up Array Elements By Group

clearly the massive differences between tin2 and tin3 must at least alert you that something is wrong with the benchmark... Frankly I have no idea what goes on once you start eval-ing strings in the Benchmark module, but from the presented examples, something seems suspicious.

Here is a benchmark by me

use Benchmark qw(cmpthese); my @arr = map {rand} 0..900000; my @output = map {rand} 0..300000;; Benchmark::cmpthese(10,{ tin1 => sub { $output[int($_/3)] += $arr[$_] for 0..@arr-1; }, tin2 => sub { $output[$_] = $arr[3*$_] + $arr[3*$_+1] + $arr[3*$_+2] for 0..@arr/3; }, tin3=> sub { my $b = 0; for (0..@arr /3) { $output[$_] = $arr[$b] + $arr[$b+1] + $arr[$b +2]; $b += 3; } } });
With result
$ perl ../../../perlmonks/bench-trip.pl Rate tin1 tin2 tin3 tin1 1.81/s -- -41% -46% tin2 3.08/s 70% -- -9% tin3 3.39/s 87% 10% --

Replies are listed 'Best First'.
Re^4: Summing Up Array Elements By Group
by ikegami (Patriarch) on Jan 21, 2009 at 12:59 UTC

    clearly the massive differences between tin2 and tin3 must at least alert you that something is wrong with the benchmark...

    The problem is that his $b kept growing. You fixed it by readding my $b = 0;.

    Which leads to two the second problem: He didn't use warnings. They don't add a penalty, so use them! If they did add a penalty, all the more reason to use them just like in the real code.

    By the way,
    $output[int($_/3)]
    can be written as
    $output[$_/3]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found