Reputation: 254
I have some code:
for my $t (1..$threads) {
push @threads, threads->create(\&action_, $t);
}
for my $t (@threads) {
$t->join();
}
sub action_ {
while (@sites) {
my $url = shift @sites;
for my $data1 (@data) {
for my $data2 (@data2) {
something($url, $data1, $data2);
}
}
}
}
How to get speed(number of calls method "something") per second(or minute)? Thanks.
Upvotes: 1
Views: 93
Reputation: 58731
If you're looking to do it yourself quickly, a gross estimate can be obtained like so:
use List::Util qw(sum);
use feature qw(say);
...
my $t0 = time(); # See also Time::HiRes
threads->create(\&action_) for 1 .. $nthreads;
my $nsomething = sum map { $_->join } threads->list();
my $elapsed = time() - $t0;
say "Among $nthreads threads we called something() ",
($nsomething/$elapsed),
" times per second";
sub action_ {
my $n;
while (...) { ... something(); $n++; ... }
return $n;
}
If that's not fine enough, you might have each call to something()
push the time it was invoked on to a threads::shared::share()
d array — or in a thread-local array that you collect after the join(), or printed to a mutex-protected filehandle or whatever. Then you can do whatever you like with the time series you've generated.
Upvotes: 2
Reputation: 272
Perl has a number of benchmarking modules in CPAN. Take a look at: http://perldoc.perl.org/Benchmark.html
This explains it well; basically the code you want to benchmark is wrapped up in a subroutine and you're free to count or compare different routines.
The method I use most commonly is under the heading 'Methods' and 'New'.
Upvotes: 2