TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (1) Thread Tools Search this Thread Display Modes
Old 09-14-2007, 11:14 PM   1 links from elsewhere to this Post. Click to view. #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Smile Calculating the Execution Time

The execution time is perhaps one of the most crucial element to your scripts. Very few individuals are going to want to wait around while your PHP script performs several server-intensive actions. The more server-intensive you make it, the more the user has to wait for those actions to complete before displaying the HTML.

Although this isn't an article on how to make your scripts more efficient. It is an article on how you can monitor the execution time and identify bottlenecks!

The execution time can be determined by calculating the microtime at the beginning and at the end of your PHP script. I am using TalkPHP's timer class as the example here - this can be downloaded for free, see the link at the end of the article.

PHP Code:
public function start()
{    
    
$iMicrotime microtime();
    
$iMicrotime explode(' '$iMicrotime);
    
$this->m_iStartTime $iMicrotime[1] + $iMicrotime[0];

The start function calculates the microtime. Now, microtime creates 2 blocks of numbers which we reverse and assign to our member variable.

PHP Code:
public function stop()
{
    
$iMicrotime microtime();
    
$iMicrotime explode(' '$iMicrotime);
    
$this->m_iEndTime $iMicrotime[1] + $iMicrotime[0];

The stop function is almost identical in its approach and simply assigns the returned data from the microtime function to a different member variable.

Once we have the 2 times stored in member variables, we can calculate how long the execution time took. Keep in mind that the start function would be called at the beginning and then stop at the very end. Our display function will then deduct 1 from the other to calculate the execution time.

PHP Code:
$iTotalTime $this->m_iEndTime $this->m_iStartTime
We deduct the start time from the end time. The variable the result was assigned to will now contain our execution time. Bear in mind that this method, using microtime, is extremely precise.

Once we have done that we can echo out our results. The result would be something like the following:

Quote:
Script execution took a total time of 0.000140190124512 seconds
Download: If you wish to download the simple yet efficacious execution time script to use in your projects, you may download it here.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/tips-tricks/1126-calculating-execution-time.html
Posted By For Type Date
Untitled document This thread Refback 01-04-2008 10:52 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:44 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design