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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: My First POE - Simple Parallel Programming by BrowserUk
in thread My First POE - Simple Parallel Programming by monkfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.