#!/usr/bin/perl use strict; use warnings; use Carp qw(confess); my @commands = map { "echo $_; sleep 2"; } qw(a b c d e); #does first, waits two seconds, does the second, waits two seconds, etc. (should take about ten seconds) #for my $bash_command ( @commands ) { # time_bash_command($bash_command); #} #does commands in parallel. (should take about two seconds) my $parallel_running_command = parallelize_bash_commands([@commands]); $parallel_running_command = parallelize_bash_commands( { a => 'sleep 2', b=> 'sleep 2', c => 'sleep 2' } ); sub parallelize_bash_commands { return join '', map { "( ( $_ ) & );" } @{ $_[0] }; #return join '', map { "( ( $_ ) & );" } @{ my $input = shift or die "no input" }; # alternative -- also works, and checks if you forgot to pass in a var. time_bash_command($parallel_running_command); } sub time_bash_command { my $command = shift or die "no command"; $command = "time `$command`"; print "$command\n"; print `$command`; }