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. |