Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use 5.16.1;
- use Thread::Pool::Simple;
- use threads::shared;
- use Redis;
- use Time::HiRes qw(time);
- use constant {
- OBJS_N=>2000,
- SLICE_N=>10,
- };
- my %resT :shared;
- my %resS;
- my $r=Redis->new;
- $r->flushdb;
- $r->set("s$_",int(rand(2000))) for 0..1999;
- $r->quit;
- undef $r;
- my $pool=Thread::Pool::Simple->new(
- 'min'=>4,
- 'max'=>128,
- 'do'=>[sub {
- return unless my @jobs=@_;
- my $r=Redis->new;
- for my $oid ( map 's'.$_, @jobs ) {
- $r->get($oid, sub { $resT{$oid}=shift })
- }
- $r->wait_all_responses
- }],
- );
- my @id;
- my $slice=OBJS_N/SLICE_N;
- my $startTick=time();
- $id[$_]=$pool->add(($slice*$_)..($slice*($_+1)-1)) for 0..(SLICE_N-1);
- $pool->join;
- my $elapT=time()-$startTick;
- say 'Estimated with threads: ', $elapT;
- $startTick=time();
- $r=Redis->new;
- for my $oid (map 's'.$_, 0..1999) {
- $r->get($oid, sub { $resS{$oid}=shift })
- }
- $r->wait_all_responses;
- my $elapS=time()-$startTick;
- say 'Estimated with async-only: '.$elapS;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement