Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Estimating computation time perl scripts

by reaper9187 (Scribe)
on Dec 27, 2012 at 12:42 UTC ( #1010516=perlquestion: print w/ replies, xml ) Need Help??
reaper9187 has asked for the wisdom of the Perl Monks concerning the following question:

Hi ,..
Is it possible to compute or estimate the execution time for a particular perl script..?? (i'm not talking about time() function or the Time::HiRes module since we need to execute it code once to calculate the time taken ). Is it possible to do that dynamically while the code is running.. ??? I was thinking of using it to display a progress bar ..
br/> Please help

Comment on Estimating computation time perl scripts
Re: Estimating computation time perl scripts
by space_monk (Chaplain) on Dec 27, 2012 at 12:54 UTC

    Well, estimation is a tricky business. Just ask the developers of Microsoft Windows file copy widget! :-)

    In order to estimate how long a process has left to take, you have to have some metric on which to base your estimation on. e.g. number of files copied against number left to copy. Even here, this can be a tricky business, as one huge file amongst lots of small files can ruin your estimation, so collection of file sizes might be a way to get a better estimate, but such an estimate would have to bear in mind the system characteristics. In other words, you need to choose the metrics you use to do your estimation carefully and work out what can possibly go wrong.

    You can then ask yourself is how precise do you want your estimate to be. Do you want a "guess" or a precisely calculated estimate? If the latter, is the time spent gathering the data required for a precise estimate going to take more time than it is worth? For example, collecting the number of files you have to copy would produce a good guess, but collecting their file sizes would produce a better estimate but would take longer. And of course collecting their file sizes may or may not be relevant. Copying a file from one directory on a disk to another location is a simple edit to a directory, and is (probably) independent of file size, whereas copying a file on a network drive to another network drive has entirely different overheads.

    A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: Estimating computation time perl scripts
by Anonymous Monk on Dec 27, 2012 at 13:01 UTC
    This is how progress bars work
    my @files = qw/ a b c d e f g/; my $total_steps = int @files; my $step = 0; for my $file ( @files ){ $step ++; print "\rStep $step / $total_steps\n"; select undef, undef, undef, 0.3; } print "Done\n"; __END__ Step 1 / 7 Step 2 / 7 Step 3 / 7 Step 4 / 7 Step 5 / 7 Step 6 / 7 Step 7 / 7 Done

      Close, but it's a bit more like this:

      $ cat t.pl my @files = qw/ a b c d e f g /; my $total_steps = int @files; my $step = 0; for my $file ( @files ){ $step ++; my $t = rand; $t *= 3 while $step / $total_steps - .01 > rand; print "$t\n"; sleep $t; print "\rStep $step / $total_steps\n"; select undef, undef, undef, 0.3; } print "Done\n"; $ perl t.pl 0.692567903622152 Step 1 / 7 1.5466213811873 Step 2 / 7 1.80846504071083 Step 3 / 7 0.564190293187867 Step 4 / 7 2.93620287911233 Step 5 / 7 0.822297130956301 Step 6 / 7 1.016940946008e+38 ^C

      ... according to my son who's trying to download a game from Steam.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Re: Estimating computation time perl scripts
by BrowserUk (Pope) on Dec 27, 2012 at 13:02 UTC

    Most every long running program contain repetition in the form of loops, or recursion, or a set of work items to be processed; and the number of repetitions are normally known, or are quickly estimable.

    If you time the first repetition and multiply by the known or first estimation of the repetition count, you can approximate the total time and derive a %complete.

    As the time taken for each repetition can vary, you can time each successive repetition and adjust the percentage dynamically.

    You'll get better/more specific suggestions if you give us more information about the nature of your program.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Estimating computation time perl scripts
by Anonymous Monk on Dec 27, 2012 at 13:23 UTC
    The only thing that a progress-bar really has to do, is to move, now and then.
Re: Estimating computation time perl scripts
by reaper9187 (Scribe) on Dec 27, 2012 at 13:57 UTC
    Thank you everyone for the quick comments,... I'm not looking to for a very precise measure of the computation time .. Jus a type of guess that can give the user a fair idea abt it .. As for the code, its really long, so i'll jus explain the process flow

    The program has a lot of subroutines and performs comprehensive checks on multiple files simultaneously ( about 6 files in all .. total size : approx 9 MB .... ) I knw how to program the progress bar., its jus the count (total time )thats a bit of a worry ... Help appreciated..!!
      get info about system (like disk speed, disk buffer size ), multiply by size of files, then guess

      Most checks on 9MB of files should be over fast enough not to need a progress bar nowadays.....

      A Monk aims to give answers to those who have none, and to learn from those who know more.
        Wish it was so quick .. The entire process takes about a 2-3 mins ... Hence the request.... Thanks again .:)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010516]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-08-21 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (127 votes), past polls