You've got a POE solution. Here's what the threads solution looks like:
#!/usr/bin/perl use warnings; use strict; use threads; sub Function_1 { my $up_bound1 = shift; my $total_f1; foreach my $in (0..$up_bound1) { $total_f1 += $in/3; } return $total_f1; } sub Function_2 { my $up_bound2 = shift; my $total_f2; foreach my $in (0 .. $up_bound2) { $total_f2 += $in; } return $total_f2; } my $up_bound = 10; my $t = threads->new( \&Function_1, $up_bound ); my $sum = Function_2( $up_bound ) + $t->join; print "The total is $sum"; __END__ c:\Perl\test>junk6 The total is 73.3333333333333
However, unless you have multiple CPUs, that will not run any faster than simply doing the two sequentially. This is a cpu bound process, and unless you have multiple cpus, both calculations will be time-sliced on the same cpu, and that will probably take longer than running them sequentially due to the overhead of task switching.
That said, the POE solution will never run more quickly. Regardless of whether you have multiple cpus or not. Even if there are multiple cpus, it will not make use of them. It will simply be time-slicing your single threaded process, and will run more slowly because of it, and all that extra complexity will have bought you nothing. POE will only benefit you performance-wise if your process is doing a lot of IOwaits which it can utilise to do other processing. POE is very clever, but it isn't designed for cpu intensive processing.
In reply to Re: My First POE - Simple Parallel Programming
by BrowserUk
in thread My First POE - Simple Parallel Programming
by monkfan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |