http://qs1969.pair.com?node_id=1126078

karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

While playing around with Parallel::ForkManager i stumbled over Re^3: Fork vs pThreads where BrowserUk wrote:

"...running 50 tasks concurrently on 4 cpus will take longer than running those same 50 tasks; but only 4 at any given time"

This sounds logical.

I wrote this:

#!/usr/bin/env perl use strict; use warnings; use Math::BigInt; use Parallel::ForkManager; use Time::HiRes qw ( time ); use feature qw(say); use Iterator::Simple qw(iter); my @numbers = ( 1 .. 2000 ); my $processes = shift; my $tmp = q(/tmp/); my $pm = Parallel::ForkManager->new( $processes, $tmp ); my %data = map { $_ => undef } @numbers; say qq(numbers: ), scalar @numbers; say qq(processes: $processes); $pm->run_on_finish( sub { my ( $id, $result ) = ( $_[2], $_[5] ); $data{$id} = ${$result} } ); my $start=time; my $iterator = iter(\@numbers); while(defined( my $number = $iterator->())) { $pm->start($number) and next; my $factorial = Math::BigInt->bfac($number); $pm->finish( 0, \$factorial ); } $pm->wait_all_children; say qq(fork: ), time - $start; %data = map { $_ => undef } @numbers; $start = time; for my $number (@numbers) { my $factorial = Math::BigInt->bfac($number); $data{$number} = $factorial; } say qq(for: ), time - $start;

I think the results are quite surprising:

karl@host:~$ ./forker.pl 50 numbers: 2000 processes: 50 fork: 22.5553870201111 for: 33.7985281944275 karl@host:~$ ./forker.pl 4 numbers: 2000 processes: 4 fork: 501.324076890945 for: 33.1997199058533

I assume (and hope) there is nothing wrong with my code but i have the vague feeling that i make some false assumptions or jump to some wrong conclusions.

What is my error in reasoning (if any)?

Update:

Many thanks to all for this interesting thread.

Update 2: Please see Allow for sub-second resolution #5 and Changes.

Thank you very much for any hints and best regards, Karl

«The Crux of the Biscuit is the Apostrophe»