#! 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;