How to measure the speed of code written in PHP?

How can I say which class of many (which do the same job) are faster to complete? is there any software to measure this?

+84
performance php testing
Jul 29 '09 at 13:20
source share
10 answers

You have (at least) two solutions:

Pretty "naive" uses microtime (true) before and after part of the code to find out how much time has passed during its execution; other answers said that they gave examples already, so I did not speak anymore.

This is a good solution if you want to compare a couple of instructions; for example, to compare two types of functions, for example - it is better if done thousands of times to make sure that any "disturbing element" is averaged.

Something like this, so if you want to know how long it will take to serialize the array:

$before = microtime(true); for ($i=0 ; $i<100000 ; $i++) { serialize($list); } $after = microtime(true); echo ($after-$before)/$i . " sec/serialize\n"; 

Not perfect, but useful, and it doesn't take long to set up.





Another solution that works pretty well if you want to determine which function takes a lot of time throughout the script is to use:

  • Xdebug extension for generating profiling data for script
  • Software that reads profiling data and presents you something readable. I know three of them:
    • Webgrind ; web interface; should work on any Apache + PHP server
    • WinCacheGrind ; only on windows
    • KCacheGrind ; possibly only Linux and Linux-like; What I prefer, btw

To get profiling files, you need to install and configure Xdebug; see the PHP Script Profiling page in the documentation.

What I usually do does not include the default profiler (it generates quite large files and slows down), but uses the ability to send the XDEBUG_PROFILE parameter as GET data to activate the page-only profiling I need.
The profiling-related part of my php.ini is as follows:

 xdebug.profiler_enable = 0 ; Profiling not activated by default xdebug.profiler_enable_trigger = 1 ; Profiling activated when requested by the GET parameter xdebug.profiler_output_dir = /tmp/ouput_directory xdebug.profiler_output_name = files_names 

(Read the documentation for more information)

This screenshot is from a C ++ program in KcacheGrind: http://kcachegrind.sourceforge.net/html/pics/KcgShot3Large.gif http://kcachegrind.sourceforge.net/html/pics/KcgShot3Large.gif
You will get exactly the same thing with PHP scripts ;-)
(With KCacheGrind, I mean: WinCacheGrind is not as good as KCacheGrind ...)

This allows you to get a great overview of what takes time in your application - and sometimes it definitely helps to find the function. which slows everything down ^^

Note that Xdebug calculates the CPU time spent by PHP; when PHP expects a response from the database (for example), it does not work; just waiting. Therefore, Xdebug will think that a database query will not take much time!
This should be profiled on SQL server, not PHP, so ...


Hope this is helpful :-)
Enjoy!

+136
Jul 29 '09 at 20:11
source share

For quick things, I do this (in PHP):

 $startTime = microtime(true); doTask(); // whatever you want to time echo "Time: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n"; 

You can also use a profiler, for example http://xdebug.org/ .

+29
Jul 29 '09 at 13:30
source share

I made a simple time class, maybe it is useful to someone:

 class TimingHelper { private $start; public function __construct() { $this->start = microtime(true); } public function start() { $this->start = microtime(true); } public function segs() { return microtime(true) - $this->start; } public function time() { $segs = $this->segs(); $days = floor($segs / 86400); $segs -= $days * 86400; $hours = floor($segs / 3600); $segs -= $hours * 3600; $mins = floor($segs / 60); $segs -= $mins * 60; $microsegs = ($segs - floor($segs)) * 1000; $segs = floor($segs); return (empty($days) ? "" : $days . "d ") . (empty($hours) ? "" : $hours . "h ") . (empty($mins) ? "" : $mins . "m ") . $segs . "s " . $microsegs . "ms"; } } 

Using:

 $th = new TimingHelper(); <..code being mesured..> echo $th->time(); $th->start(); // if it the case <..code being mesured..> echo $th->time(); // result: 4d 17h 34m 57s 0.00095367431640625ms 
+8
Jul 30 '15 at 14:02
source share

I used XHProf recently http://pecl.php.net/package/xhprof . It was originally developed by Facebook and comes with a decent web interface.

+4
Jul 30 '09 at
source share

Here is a direct answer to your question

is there any software to measure this?

Yes there is. I wonder why no one has mentioned this yet. Although the answers above seem to be great for a quick check, they are not scalable in the long run or for a larger project.

Why not use the Application Performance Monitoring Tool (APM), which is built specifically for this and much more. Check out NewRelic, AppDynamics, Ruxit (everyone has a free version) to control runtime, resource usage, and bandwidth of each application at the method level.

+4
Oct. 15 '15 at 15:18
source share

I would like to share with you my own function, which I use to measure the speed of any existing function with up to 10 arguments:

 function fdump($f_name='', $f_args=array()){ $f_dump=array(); $f_result=''; $f_success=false; $f_start=microtime(); $f_start=explode(' ', $f_start); $f_start=$f_start[1] + $f_start[0]; if(function_exists($f_name)){ if(isset($f_args[0])&&is_array($f_args[0])){ if($f_result=$f_name($f_args)){ $f_success=true; } } elseif(!isset($f_args[1])){ if($f_result=$f_name($f_args[0])){ $f_success=true; } } elseif(!isset($f_args[2])){ if($f_result=$f_name($f_args[0],$f_args[1])){ $f_success=true; } } elseif(!isset($f_args[3])){ if($f_result=$f_name($f_args[0],$f_args[1],$f_args[2])){ $f_success=true; } } elseif(!isset($f_args[4])){ if($f_result=$f_name($f_args[0],$f_args[1],$f_args[2],$f_args[3])){ $f_success=true; } } elseif(!isset($f_args[5])){ if($f_result=$f_name($f_args[0],$f_args[1],$f_args[2],$f_args[3],$f_args[4])){ $f_success=true; } } elseif(!isset($f_args[6])){ if($f_result=$f_name($f_args[0],$f_args[1],$f_args[2],$f_args[3],$f_args[4],$f_args[5])){ $f_success=true; } } elseif(!isset($f_args[7])){ if($f_result=$f_name($f_args[0],$f_args[1],$f_args[2],$f_args[3],$f_args[4],$f_args[5],$f_args[6])){ $f_success=true; } } elseif(!isset($f_args[8])){ if($f_result=$f_name($f_args[0],$f_args[1],$f_args[2],$f_args[3],$f_args[4],$f_args[5],$f_args[6],$f_args[7])){ $f_success=true; } } elseif(!isset($f_args[9])){ if($f_result=$f_name($f_args[0],$f_args[1],$f_args[2],$f_args[3],$f_args[4],$f_args[5],$f_args[6],$f_args[7],$f_args[8])){ $f_success=true; } } elseif(!isset($f_args[10])){ if($f_result=$f_name($f_args[0],$f_args[1],$f_args[2],$f_args[3],$f_args[4],$f_args[5],$f_args[6],$f_args[7],$f_args[8],$f_args[9])){ $f_success=true; } } } $f_end=microtime(); $f_end=explode(' ', $f_end); $f_end=$f_end[1] + $f_end[0]; $f_time=round(($f_end - $f_start), 4); $f_dump['f_success']=$f_success; $f_dump['f_time']=$f_time; $f_dump['f_result']=$f_result; var_dump($f_dump);exit; //return $f_result; } 

Example

 function do_stuff($arg1='', $arg2=''){ return $arg1.' '.$arg2; } fdump('do_stuff',array('hello', 'world')); 

Returns

  array(3) { ["f_success"]=> bool(true) ["f_time"]=> float(0) //too fast... ["f_result"]=> string(11) "hello world" } 
+3
Jan 31 '14 at 10:46
source share

If you want to quickly check the performance of the framework, you can add the index.php file

 //at beginning $milliseconds = round(microtime(true) * 1000); //and at the end echo round(microtime(true) * 1000) - $milliseconds; 

Every time you get a runtime in milliseconds . Since microseconds are not very useful when testing the framework.

+3
Sep 18 '15 at 8:00
source share

If this is something that can be tested outside of the Web context, I just use the Unix time command.

+2
Jul 29 '09 at 13:22
source share

Zend Studio has built-in profiling support using XDebug or ZendDebugger. It will profile your code, telling you how long each function has been running. This is a fantastic tool for determining where your bottlenecks are.

+2
Jul 29 '09 at 17:22
source share

You can use basic things like saving timestamps or microtime () before and after an operation to calculate the time needed. This is easy to do, but not very accurate. Perhaps the best solution is Xdebug , I have never worked with it, but it seems to be the most famous PHP debugger / profiler I can find.

0
Jul 29 '09 at 13:30
source share



All Articles