This is an ideal application for threading if that option is available to you.

#! perl -sw use strict; require 5.008; use threads 'yield'; use threads::shared; use vars qw[$ITERS]; $ITERS ||= 10000; $|=1; my $percent : shared = 0; sub progress{ print '[', ' ' x 80, ']', $percent; while( $percent < 1 ) { printf "\r[%s%s] %5.2f%% ", '#' x (80*$percent), ' 'x(80*(1-$p +ercent)), 100*$percent; yield; } print "\n"; return; } sub TakesALongTime{ my $in = 0; my $max = shift || 10000; for my $count ( 1 .. $max ) { $percent = $count/$max; rand () ** 2 + rand () ** 2 < 1 && $in ++ ; yield; } return sprintf '%f', 4 * $in / $max; } my $thread = threads->create('progress'); my $PI_approx = TakesALongTime($ITERS); $thread->join; print "After $ITERS iterations, pi was approximated to be: $PI_approx\ +n"; __END__ D:\Perl\test>261528 -ITERS=100000 [##################################################################### +##########] 100.00% After 100000 iterations, pi was approximated to be: 3.136440

Not a great demo, but if any of it needs explaination, please ask. (See Monte Carlo approximation of PI for Abigail's Monte Carlo approximation of PI snippet.)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: User Feedback for Subroutines with Long Execution Time by BrowserUk
in thread User Feedback for Subroutines with Long Execution Time by arunhorne

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.