As I expressed in the introduction, one of the simplest ways to create a basic benchmarking mechanism relies on using the “microtime()” built-in PHP function. Just in case you didn’t know, this function returns the current Unix timestamp, which additionally appends the corresponding microseconds to the output. According to the definition from the PHP manual, the mentioned function returns the string "msec sec," where sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and "msec" is the microseconds part. Both portions of the string are returned in units of "seconds.” In simple terms, the syntax for the “microtime()” function is as follows: mixed microtime ( [bool get_as_float] ) Additionally, when you’re using PHP 5, the function accepts the optional “get_as_float” parameter, which simply returns the complete timestamp as a float number. As you may have guessed from the above definition, this function is quite handy for developing a simple benchmarking mechanism that only requires a few lines of code. Therefore, here is a basic custom function that uses “microtime().” Take a look at the definition of this function: // define 'getMicrotime()'function in PHP 4 function getMicrotime(){ list($useg,$seg)=explode(' ',microtime()); return ((float)$useg+(float)$seg); } The “getMicrotime()” function that I defined above demonstrates a classic use of “microtime()” to construct a basic benchmarking system. It’s one of the most common implementations of the function that I ever saw. That being said, let me go one step further and show you a simple example of how to use the previous “getMicrotime()” function inside of a benchmarking script. Here is the pertinent code sample: $startTime=getMicrotime(); // do nothing for a while usleep(100); $endTime=getMicrotime(); $totalTime=$endTime-$startTime; echo 'Time spent in doing nothing was '.$totalTime.' seconds'; In this case, I simply applied my common sense to create a primitive PHP4-based benchmarking script. As you saw, the example first calls up the “getMicrotime()” function and stores the corresponding timestamp. Then it does nothing for a while. Finally, it displays the elapsed time via another function call. Very simple, isn’t it? Of course, the previous example would be rather incomplete if I don’t show you the respective output generated by the above script, so here it is: Time spent in doing nothing was 0.00837111473083 seconds Okay, at this point, I created a fairly precise benchmarking function that can be used for timing different portions of code, or even complete applications. I do not mean that the prior example can be used freely in production environments, but it shows, in a nutshell, how easy it is to create these kind of scripts using only the “microtime()” function. In the following section, I’ll show you how to use this helpful function with PHP 5. Thus, you can spot the difference between the different versions of the language. To learn more on this topic, all you have to do is click on the link that appears below and read the next few lines. See you there.
blog comments powered by Disqus |
|
|
|
|
|
|
|