in reply to Re: how to multiply an array of number as fast as posible
in thread how to multiply an array of number as fast as posible

The problem with your benchmark is the calculation moves into floating point exception handling very early on.

If you avoid that by adding logs, using threads improve performance by 65%:

#! perl -slw use strict; use threads; use Time::HiRes qw[ time ]; { my $start = time; my $t = 0; $t += log( $_ ) for 230000..900000; printf "%.15f\n", $t;; printf "Took %.6f\n", time() - $start; } { my $start = time; my $thr = async { my $t = 0; $t += log( $_ ) for 230000..565000; return $t; }; my $t = 0; $t += log( $_ ) for 565001..900000; $t += $thr->join; printf "%.15f\n", $t;; printf "Took %.6f\n", time() - $start; } __END__ C:\test>junk32 8829606.110849546300000 Took 0.132204 8829606.110849652400000 Took 0.080650

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^3: how to multiply an array of number as fast as posible
by kennethk (Abbot) on Sep 02, 2010 at 17:32 UTC
    But at the same time, you've just added a transcendental function to every loop iteration, which is a much more expensive operation than a multiplication. As I pointed out in Re^3: how to multiply an array of number as fast as posible. A more fair comparison would be scaling the operations so that we avoid INFs in the first place:

    #!/usr/bin/perl use strict; use warnings; use Benchmark qw':all :hireswallclock'; use threads; cmpthese(10, { 'for' => sub { my $k = 1; for (230000 .. 900000) { $k *= 1 + $_* 0.0000000001; } #print "for: $k\n"; }, 'threads' => sub { my $thr2 = async { my $k = 1; for (565001 .. 900000) { $k *= 1 + $_* 0.0000000001; } return $k; }; my $k = 1; for (230000 .. 565000) { $k *= 1 + $_* 0.0000000001; } $k *= $thr2->join(); #print "threads: $k\n"; } });

    I've replaced one multiplication per iteration with 2 multiplications and an addition (plus the store), which should shift the balance toward threads. And yet, the benchmark comes out in favor of single-threaded operation:

    Rate threads for threads 2.92/s -- -35% for 4.48/s 53% --

      Hm. Benchmarking a different calculation doesn't seem useful to me.