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»


In reply to Useful number of childs revisited [SOLVED] by karlgoethebier

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.