Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Show Page Process Time in Perl (http://www.go4expert.com/articles/page-process-time-perl-t8463/)

pradeep 25Jan2008 14:12

Show Page Process Time in Perl
 
Showing a page process time in the page footer is a common thing in many sites, some sites even take it forward by showing DB query time and processing time separately.

In this tip, we'll see how to show page process time in perl. For this purpose we'll require the perl module Time::HiRes (CPAN).

Installing Time::HiRes



You can either download the source from CPAN and then install it manually or you can use the cpan shell module to install Time::HiRes.

Code:

$cpan
 $cpan> install Time::HiRes

The Code



In array context gettimeofday() returns a two-element array with the seconds and microseconds since the epoch. Returns the floating seconds between the two times, which should have been returned by gettimeofday() . If the second argument is omitted, then the current time is used.

Code: Perl

#!/usr/bin/perl
 
 use CGI;
 use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
 use Time::HiRes qw/gettimeofday tv_interval/;
 
 use strict;
 use warnings;
 
 my $cgi = new CGI;
 my $start_time = [gettimeofday];
 
 print $cgi->header(),'<h3>Page process timer in perl</h3>';
 
 ## run a dummy loop
 ## you actual code will come here
 for(my $i=0;$i<1000000;$i++)
 {
     my $cool = 1;
 }
 
 my $total_time = tv_interval($start_time);
 
 printf('Page proccessed by Perl %s in %.02f seconds',$],$total_time);

Using the same logic you can show SQL query time, etc. and customize according to your needs.


All times are GMT +5.5. The time now is 04:13.