Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track execution time of each line / block of lines / methods in PHP?

Tags:

php

ide

workflow

Is there a best practice (IDE-based?), a PHP/bash script or something else to

  • get the execution time of a line
  • get the execution time of a block of lines (for example line 17 to 42)
  • get the execution time of a certain function or method

in PHP ? I'm currently stuck with self-built solutions that use microtime(), but that's a dirty, unhandy and slow method to do so. I'm especially interested in solutions with the major PHP IDEs like Eclipse, Netbeans, PHPStorm and VIM. A perfect solution would be a tool that tracks an entire application and provides execution time tracking for each line, each method and each custom block of actions.

I'll provide 50 bounty for a good answer.

like image 688
Sliq Avatar asked Jul 15 '13 10:07

Sliq


People also ask

How to measure time of execution in PHP?

Clock time can get using microtime() function. First use it before starts the script and then at the end of the script. Then using formula (End_time – Start_time). The microtime() function returns time in seconds.

How to create running time in PHP?

PHP time() Function$t=time(); echo($t . "<br>"); echo(date("Y-m-d",$t));

What is the maximum execution time in PHP?

One important aspect of PHP programs is that the maximum time taken to execute a script is 30 seconds. The time limit varies depending on the hosting companies but the maximum execution time is between 30 to 60 seconds.


1 Answers

This is what you are looking for: http://xdebug.org/docs/profiler. You can set up netbeans to attach to php process, than run scripts step by step, watch current variables content and analyze its running times.

Just add dll to other php extension, configure by php ini:

; xdebug
zend_extension = "C:\php\v5.4\ext\php_xdebug-2.2.3-5.4-vc9-nts.dll"
xdebug.remote_enable=on
;xdebug.remote_log="/var/log/xdebug.log"
xdebug.remote_host=localhost
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.profiler_enable=1
xdebug.profiler_output_dir="C:\dev\profilling\"

Netbeans setup:

enter image description here

Then press Ctrl+F5 in netbeans (debugging). Than you can run script step by step and watch current variables values:

enter image description here

To browse generated profilers log use http://sourceforge.net/projects/precompiledbin/ or http://sourceforge.net/projects/wincachegrind/ in windows, or http://kcachegrind.sourceforge.net/html/Home.html for linux.

like image 84
Fanda Avatar answered Sep 22 '22 19:09

Fanda