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
| [reply] |
END {
my $elapsed_seconds = time - $^T;
# Do something with $elapsed_seconds
# e.g., print qq(<!-- Script took ${elapsed_seconds}s to run -->);
}
| [reply] [d/l] |
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
| [reply] |
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.$$";
| [reply] [d/l] |
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' );
| [reply] [d/l] [select] |
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.
| [reply] [d/l] |
#!/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
$
| [reply] [d/l] [select] |