1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Show Page Process Time in Perl

Discussion in 'Perl' started by pradeep, Jan 25, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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:
     #!/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.
     

Share This Page