in reply to Re: while reading a file, transfer the same data to two different processes.
in thread while reading a file, transfer the same data to two different processes.

Well, every thread has some overhead too:

use strict; use warnings; use threads; my $str = 'x' x 32_000_000; sub fork_wait { my $pid = fork; if ($pid) { wait; } else { exit 0; } } sub create_thread_join { threads->create( sub { } )->join; } use Benchmark qw( cmpthese ); cmpthese - 3, { Forks => \&fork_wait, Threads => \&create_thread_join, }; __END__ Rate Threads Forks Threads 27.7/s -- -73% Forks 103/s 272% --

Even without this 32M string threads not win. This is Linux, perhaps on Windows you will get other result.

  • Comment on Re^2: while reading a file, transfer the same data to two different processes.
  • Download Code

Replies are listed 'Best First'.
Re^3: while reading a file, transfer the same data to two different processes.
by BrowserUk (Patriarch) on May 20, 2010 at 20:27 UTC
    perhaps on Windows you will get other result.

    On windows, those two pieces of code are all but identical (except the fork version leaks scalars). Hence, there is little difference between them. I'd be interested to see the results without the 32M scalar if you've a moment?

      Without 32M scalar it outputs:

      Rate Threads Forks Threads 230/s -- -0% Forks 231/s 0% --

        Thanks. If you've the inclination to indulge me a little further, what results do you get from the following?

        use strict; use warnings; use threads; my $str = 'x' x 32_000_000; sub fork_wait { my $pid = fork; if ($pid) { wait; } else { my $l = length( $str ); exit 0; } } sub create_thread_join { threads->create( sub { my $l = length( $str ) } )->join; } use Benchmark qw( cmpthese ); cmpthese - 3, { Forks => \&fork_wait, Threads => \&create_thread_join, };