views:

281

answers:

5

I have a script that is running slowly and I was wondering how I could time it and see which fixes improve the speed. Is there some sort of a PHP timer?

Update: Thought I should clarify... The script that is running slowly is my mailing list script that sends an email to everyone on the list. So I am looping through an array of subscribers and sending them an email one by one. It's so slow that I thought it would be interesting to see how long it took to iterate though the array. So I'm looking for a quick and easy way to test that.

+1  A: 

You can measure the loading time of your page and adjust where needed.

I like websites like http://www.phpbench.com/ which tell you more about the basic needs in php.

Offcourse writing php using Zend Framework doesn't really say anything about your code actually. Do you follow the zend coding standards ?

http://www.php.net/getrusage might be usefull for your timing issue.

Good luck !

edit : extra link for the getrusage function ( reffered to on the php.net page as well ).

edit 2 : For your mailing list you could check phpbench.com for example with the count before the loop which saves some time.

How are you sending your mails ? That could be a bottleneck.

Good luck !

mhd
I only mentioned Zend Framework in case there was something built in that I didn't know about
Andrew
+1  A: 

The best thing I know about, and use all the time, is to use a profiler. My profiler of choice is Xdebug which has a built-in profiler, and getting it installed in your PHP setup is fairly easy. Once up and running, it can give you all sorts of debugging and profiling information, and the output format of Xdebug can be read by the amazing kCacheGrind tool (visualizing and reporting on the stack and graphing your application flow) if you're a happy Linux camper. What would I do without these tools!?

Also, there's a few pitfalls in terms of speed when it comes to the Zend Framework, mostly associated with not using the Zend_Cache class enough, or convoluted class hiearchies where you possibly should use the Zend_Registry class.

Happy profiling.

AlexanderJohannesen
It's kind of weird implying that for ZF to be fast one would need to use Zend_Cache. And Zend_Registry is not a solution to convoluted architecture, au contraire.
Ionuț G. Stan
It's weird that when people use the Zend Framework they make it go slow because they aren't using an important part of it for making it go faster? :)Also, "convoluted" : 1. twisted; coiled. 2. complicated; intricately involvedSo, when your program is convoluted you can untangle complexities by shifting them into the Zend_Registry. That's its purpose.
AlexanderJohannesen
A: 

Download and install Xdebug, adjust your php.ini for Xdebug and make sure Zend Debugger is not active (commented out). Check your phpinfo() to see if Xdebug works.

If you're on a XAMPP environment you should see an Image saying Zend Engine vX.X.X, [...] with Xdebug vX.X.X, [...]

Then download a profiling tool like CacheGrind, if you're on a Windows environment for example WinCacheGrind.

CacheGrind reads the cachegrin.out files which Xdebug produced somewhere in a tmp folder. Maybe xampp\tmp.

This will show you time measurements for every function/action.

tharkun
A: 

How about using a queue? Seems like a good idea to me, because as the list grows one day you'll hit the limits of the mail server. There's a popular implementation in PEAR of this queue thing, but basically all you need is a cron job and a database table.

I'm building something similar in ZF so I'm interested to see what happens!

bertbalcaen
A: 

I ended up using a timer class I found similar to this one: http://forum.codecall.net/php-tutorials/4254-php-timer-class.html

I just started a timer at the beginning of my loop and logged the duration at the end of each iteration. Found out, on average, it was taking between 3 and 4 seconds per email! Five minutes per email blast is a little too much. Time to refactor...

Andrew