in reply to Re^2: how to multiply an array of number as fast as posible
in thread how to multiply an array of number as fast as posible
#!/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% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: how to multiply an array of number as fast as posible
by BrowserUk (Patriarch) on Sep 02, 2010 at 17:56 UTC |