#! perl -slw
use strict;
use threads;
use threads::shared;
use Thread::Queue;
use Storable qw[ freeze thaw ];
use Data::Dump qw[ pp ]; $Data::Dump::WIDTH = 2000;
our $N //= 1000;
our $H //= 100;
{
my $start = Win32::GetTickCount();
my $Q = new Thread::Queue;
async{
my %h = 1 .. $H;
$Q->enqueue( \%h ) for 1 .. $N;
$Q->enqueue( undef );
}->detach;
my %h;
%h = %{ $_ } while defined( $_ = $Q->dequeue );
printf "Unshared hashrefs: %.3f\n",
( Win32::GetTickCount() - $start ) / 1000;
}
{
my $start = Win32::GetTickCount();
my $Q = new Thread::Queue;
async{
my %h = 1 .. $H;
$Q->enqueue( join $;, %h ) for 1 .. $N;
$Q->enqueue( undef );
}->detach;
my %h;
%h = split $;, $_ while defined( $_ = $Q->dequeue );
printf "join/split: %.3f\n",
( Win32::GetTickCount() - $start ) / 1000;
}
{
my $start = Win32::GetTickCount();
my $Q = new Thread::Queue;
async{
my %h = 1 .. $H;
$Q->enqueue( freeze \%h ) for 1 .. $N;
$Q->enqueue( undef );
}->detach;
my $href;
$href = thaw $_ while defined( $_ = $Q->dequeue );
printf "freeze/thaw: %.3f\n",
( Win32::GetTickCount() - $start ) / 1000;
}
{
my $start = Win32::GetTickCount();
my $Q = new Thread::Queue;
async{
my %h :shared = 1 .. $H;
$Q->enqueue( \%h ) for 1 .. $N;
$Q->enqueue( undef );
}->detach;
my $href;
1 while defined( $href = $Q->dequeue );
printf "Shared hashrefs: %.3f\n",
( Win32::GetTickCount() - $start ) / 1000;
}
####
c:\test>TQ-b -H=200 -N=10000
Unshared hashrefs: 10.857
join/split: 3.121
freeze/thaw: 0.686
Shared hashrefs: 0.265
####
c:\test>TQ-b -H=2000 -N=10000
Unshared hashrefs: 117.532
join/split: 30.482
freeze/thaw: 2.886
Shared hashrefs: 0.250