Hi,

I use a simple little CPU-Benchmark based on the perl "Benchmark" module for rough performance comparison. Now I wanted to extend it to run the same test in parallel with a given number of threads. After I
got totally unusable results, I stripped down the code for investigation:

use Time::HiRes qw( gettimeofday tv_interval );
use Benchmark qw(:all :hireswallclock);
use threads;

my $num_threads=8;
my $secs=10;
my (@bench, @thr, $total);
my $testcode=sub {
    my $sum; for (my $i=0; $i < 1000; $i++) { $sum+=$i } };
my $t0 = [gettimeofday];
for (my $i=1; $i <= $num_threads; $i++) {
    $thr[$i]= threads->create(sub { countit($secs, $testcode) });
}
for (my $i=1; $i <= $num_threads; $i++) {
    $bench[$i]=$thr[$i]->join();
    printf "%02d:       iter: %d\n", $i, $bench[$i]->iters;
    $total += $bench[$i]->iters;
}
printf "%04.1f secs iter: %d\n",
  tv_interval($t0,[gettimeofday]), $total;

For $num_threads=1 it works as expected; the higher the number of threads, the more unpredictable it behaves. Instead of running the code for 10 secs, there is a tendency that each thread runs ~($secs/$num_threads) seconds (+ occasional runaways that terminate after a totally unpredictable time).

At 1st I thought there might be some general problem between timers and threads. With the following instead of Benchmark::countit, I get the expected results:

sub countit {
    my ($secs,$code)=@_;
    my $count=0; my $start=time();
    while ((time()-$start) < $secs ) { $count++; $code->() }
    return $count;
};

Does anybody happen to have any experience with Benchmark.pm. Is it generally not usable with multiple threads or am I just doing something wrong? (Or is it maybe system dependent - I tried it on several machines
running Debian on Linux 2.6.26 - 3.2.0 amd64)

 Regards,
                     Peter


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to