rsennat has asked for the wisdom of the Perl Monks concerning the following question:

hi all

im running a perl/cgi script from the browser and see that, its taking lot of time.

how to calculate the time taken to complete the script??
though the apache server used may be slow, just wanted to calculate the time taken to run the script

thanks

  • Comment on calculate the time taken to run a script

Replies are listed 'Best First'.
Re: calculate the time taken to run a script
by davorg (Chancellor) on Nov 23, 2005 at 16:25 UTC

    Take the time at the start and end of the program and subtract the first from the second.

    You may want to look at Time::HiRes if you want sub-second accuracy.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Perl already keeps track of start time for you in $^T, so unless you're doing something really funky, you can just subtract that from the current time to get elapsed time.

      END { my $elapsed_seconds = time - $^T; # Do something with $elapsed_seconds # e.g., print qq(<!-- Script took ${elapsed_seconds}s to run -->); }

          --k.


Re: calculate the time taken to run a script
by jfroebe (Parson) on Nov 23, 2005 at 16:30 UTC

    Hi,

    Take a look at the Benchmark module once you have an idea where the slowness is. It will help you test different methods of increasing performance. A nice tutorial.

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: calculate the time taken to run a script
by Moron (Curate) on Nov 23, 2005 at 16:34 UTC
    Devel::Timer has the facilities although if it were me I'd be satisfied with just recording the timestamps and calculate the differences later:
    #!user/bin/perl # use ... statements system "echo '" . localtime() . ": starting' > /tmp/mycgi.$$"; # .. rest of script system "echo '" . localtime() . ": done' >> /tmp/mycgi.$$";

    -M

    Free your mind

Re: calculate the time taken to run a script
by wazzuteke (Hermit) on Nov 23, 2005 at 16:59 UTC
    Another method is if you are running a Linux environment and the CGI is executable from the command line, you can see the raw time (with no code changes) using the time bash command:

    #~> time ./some_script.(pl|cgi)
    Outputs:
    real 0m0.000s user 0m0.000s sys 0m0.000s
    Always a very simple and quick method of timing different utilities and things like that.

    ---hA||ta----
    print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );
Re: calculate the time taken to run a script
by holli (Abbot) on Nov 23, 2005 at 19:08 UTC
    I read your question different from my esteemed fellow monks. I think you don't want to just substract the start and end time from your script. I think you want to do two independent things:
    • calculate the time it takes to finish your script, while the script is running.
    • show that time and the current progress to the user in a CGI context
    Regarding the latter you should read CGI progress indicator which contains more links to more resources for this problem.

    To solve the first you must be aware how the problem can be split in "units" (lines of csv-file*, records of a fixed-length file/database query or one solution in a shuffle of n numbers, etc.) and how many "units" there are. If that number is undetermina(te|ble) or the processing time per "unit" varies widely, it's hard to predict the runtime reliably.

    The main algorithm is relatively simple:
    remember starttime determine total_number_of_units loop as long there is a unit to process process unit calculate elapsed time calculate average of elapsed time per unit multiply average with total number of units #this gets the predicted + runtime end loop
    I leave it to you to translate that to Perl, since you gave us no code to work with.

    Update:

    * In the case of a CSV file the number of lines is usually unknown, so it makes sense to use a Byte as a "unit" and to sum up the bytes read to calculate the average.


    holli, /regexed monk/
Re: calculate the time taken to run a script
by awohld (Hermit) on Aug 05, 2016 at 06:52 UTC
    use Timer::Runtime

    Example: duration.pl
    #!/usr/bin/perl use Timer::Runtime; print "hi\n";
    Outputs:
    $ perl duration.pl duration.pl Started: Fri Aug 5 01:48:14 2016 hi duration.pl Finished: Fri Aug 5 01:48:14 2016, elapsed time = 00:00:0 +0.000398 $