PHP Performance Measures

 

0 Comments | Write a Comment | Rate this Article | Report Article

 

Type

Tutorial for Advanced Users

Category

Webdesign & Web Development

Language

English

Author

Stefan Trost Media

Date

13.09.2011

Ratings

30

Views

2299
 
 


About the author

Stefan Trost is a developer of software and web solutions and gladly also cares about your needs and desires. Contact

Profile of Stefan Trost Media
Articles by Stefan Trost Media

Do you ask yourself how long a creation of one of your pages with PHP takes or do you want to compare several scripts and test which function works most efficiently? In this short tutorial we will explore this question and show you how you can compare and measure PHP performance.

A simple function for measuring time

In order to test the performance of a function or a script, we need the possibility of a time measurement. Just use the following short script for this.

01 $tstart = microtime(true);

02

03 ...

04

05 echo number_format(((microtime(true)-$tstart)*1000),2),' ms';

In line 01, the current time is measured with the function microtime( ) and then it is written in the variable $tstart. In line 05, we ask for the time again and subtract this time from $tstart. What remains is the time difference between line 01 and line 05. So, we have measured how much time between line 01 and line 05 is passed. In line 05 we use the number_format function, with which we produce an output such as "12.34 ms" in order to show the results of our time measurement.

Between line 01 and line 05 we can now write any script or function, we want to measure.

Measure time for the entire page creation

You might be interested in how long it takes to output an entire page in which there are perhaps multiple PHP scripts and several MySQL queries. We want to have a look at this question in the next example.

<?php
$tstart = microtime(true);
?><html>
<head>
<meta ...
...
<?php
echo 'The creation of this page took ';
echo number_format(((microtime(true)-$tstart)*1000),2),' ms';
?>

</body></html>

Again, we are using the function from our first example. The start of our time measurement, we put at the very beginning of our HTML page. Even before the <html>-Tag, we copy the current time to our variable $tstart. Neverthelesse, we see <html> at the beginning of our created HTML page, because we do not create any output in our first PHP snippet.

After that, the rest of the Internet page with all meta-tags, all other PHP scripts and MySQL queries comes. We come to the measurement shortly before the closing </body> and </html> again. Here we are using our output function for another time to output the creation time of the entire page. Of course, you can format the output a little bit, so that it better fits in your webpage. For example, you can center the output or create a DIV-box around it.

Compare scripts

If you are using scripts or functions on your homepage, you should think about what function is most suitable and works fastest and most efficiently. Because the faster a function is, the more you can run in the same time and the more page views your server can handle.

01 //echo benchmark

02 $techo = microtime(true);

03 for ($i = 0; $i<100000; $i++) print 'that is a test';

04 $techoend = microtime(true);

05

06 //print benchmark

07 $tprint = microtime(true);

08 for ($i = 0; $i<100000; $i++) print 'that is a test';

09 $tprintend = microtime(true=;

10

11 //echo times

12 echo 'echo took ';

13 echo number_format((($techoend-$techo)*1000),2),' ms';

14 echo 'print took ';

15 echo number_format((($tprintend-$tprint)*1000),2),' ms';

 

In the example we want to compare the echo with the print function. Which of these two functions is faster and more efficient, is a question often debated on various internet sites and also depends on several factors. Here we only want to use these functions as an example and compare the times print and echo are using to output a simple string.

However, it is not enough to only compare one call of each function. The random effect, for example by further processes in the computer, would be to great to come to a reasonable result. For this reason, we perform the two comparisons for 100,000 times. Here again, we get different results when repeating this procedure, but here the random effect should be not so big because we get an average value.

In lines 01 to 04, we output the sentence 100,000 times with echo and we are saving the time before and after the call. We make the same with the print function in lines 06 to 09. After that, we print the results of our measurement in lines 11 to 15.

Now you can vary the length of the sentences, you can try to output some variables or you can experiment with the function in another way. Moerover, you can use this script to test each other function or script. Simply write the things you want to test in the lines 03 and 08.

 
  
 

Comments

Have you got the same opinion like the author or do you want to add something? Here you can leave a comment.

Write a comment

You can leave an anonymous comment. If you want to write something under your name, please log in or register.



Past Comments

Nobody has written a comment on this article. You can be the first one.