#!/usr/bin/perl use strict; use warnings; use Benchmark qw':all :hireswallclock'; use threads; cmpthese(10, { 'while' => sub { my $i = 230000; my $k = 1; while ($i <= 900000) { $k *= $i++; } }, 'for' => sub { my $k = 1; for (230000 .. 900000) { $k *= $_; } }, 'threads' => sub { my $k = 1; my $thr2 = async { my $k = 1; my $i = 565001; while ($i <= 900000) { $k *= $i++; } }; my $i = 230000; while ($i <= 565000) { $k *= $i++; } $k *= $thr2->join(); } }); #### Rate threads while for threads 3.09/s -- -29% -38% while 4.33/s 40% -- -13% for 4.96/s 60% 15% --