in reply to Threads From Hell #1: How To Share A Hash [SOLVED]
Corion kindly advised my not to use Thread::Semaphore and pointed me to ... Thread::Queue.
I finally got around to putting together proof of the wisdom of Corion's advice.
This is a version of your Thread::Semaphore code, limiting to 4 concurrent threads and calculating factorials 1000! .. 2000!:
#! perl -slw use strict; use threads; use threads::shared; use Thread::Semaphore; use Math::BigInt; use Time::HiRes qw[ time ]; use Data::Dump qw[ pp ]; use feature qw(say); my %result : shared; my $semaphore = Thread::Semaphore->new(4); sub process { my $number = shift; my $factorial = factorial($number); lock %result; $result{$number} = $factorial->bsstr; $semaphore->up; } sub factorial { my $number = shift; Math::BigInt->bfac($number); } my @numbers = ( 1000..2000 ); my $start = time; my @threads = map { $semaphore->down; threads->create( \&process, $_ ); } @numbers; $_->join for @threads; my $end = time; printf "Took %.6f seconds\n", $end - $start;
Total runtime: Took 47.511607 seconds
This version does the same calculations using the same number of concurrent threads, but ditches Thread::Semaphore in favour of Thread::Queue to queue the 1000 numbers to 4 reused threads, thus saving the startup and teardown costs of 996 threads:
#! perl -slw use strict; use threads; use threads::shared; use Thread::Queue; use Math::BigInt; use Time::HiRes qw[ time ]; use Data::Dump qw[ pp ]; use feature qw(say); my %result : shared; sub process { my $Q = shift; while( my $number = $Q->dequeue ) { my $factorial = factorial($number); lock %result; $result{$number} = $factorial->bstr; } } sub factorial { my $number = shift; Math::BigInt->bfac($number); } my $start = time; my $Q = new Thread::Queue; $Q->enqueue( 1000..2000, (undef) x 4 ); my @threads = map { threads->create( \&process, $Q ); } 1 .. 4; $_->join for @threads; my $end = time; printf "Took %.6f seconds\n", $end - $start;
Total runtime: Took 31.290966 seconds; giving a 33% saving of time.
But the biggest lesson of threading, is when not to use it. This version ditches threads altogether and uses the obvious optimisation:
#! perl -slw use strict; use Math::BigInt; use Time::HiRes qw[ time ]; use feature qw(say); my $start = time; my $bn = Math::BigInt->bfac( 1000 ); my %result; $result{ 1000 } = $bn->bstr; for( 1001..2000 ) { $bn *= $_; $result{ $_ } = $bn->bstr; } my $end = time; printf "Took %.6f seconds\n", $end - $start;
Total runtime: Took 0.685503 seconds. Same results, but a 98.5% time saving over the first version above.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Threads From Hell #1: How To Share A Hash [SOLVED]
by karlgoethebier (Abbot) on May 15, 2015 at 18:34 UTC | |
|
Re^2: Threads From Hell #1: How To Share A Hash [SOLVED]
by karlgoethebier (Abbot) on May 16, 2015 at 11:31 UTC | |
by BrowserUk (Patriarch) on May 16, 2015 at 12:46 UTC | |
by karlgoethebier (Abbot) on May 17, 2015 at 09:01 UTC | |
by BrowserUk (Patriarch) on May 17, 2015 at 13:37 UTC | |
by karlgoethebier (Abbot) on May 17, 2015 at 14:29 UTC | |
|