Show Page Process Time in Perl

pradeep's Avatar author of Show Page Process Time in Perl
This is an article on Show Page Process Time in Perl 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.

 $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
 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.