Forum Moderators: coopster

Message Too Old, No Replies

Benchmarking PHP scripts

         

erikcw

2:02 am on Jul 17, 2004 (gmt 0)

10+ Year Member



I am writting a script that dynamically creates a webpage based on a large (100-200k of data) txt file. In doing so, I am beginning to become concerened about performance issues.

Can anyone share some tips on how to benchmark scripts (cpu time, memory, etc...)?

If I use explode($var, "\n") to break a variable into an array for processing (the var/file has about 11k lines of data) am I asking for trouble? Should I do this in some other way?

Thanks!

jatar_k

5:40 pm on Jul 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this for benchmarking
[webmasterworld.com...] msg 4

see how long it takes as is and then take a look at reading it line by line into an array and see if it makes a difference

ergophobe

6:56 pm on Jul 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In this really techie article [phplens.com] John Lim discusses a situation similar to yours and talks about two scripts, one that reads a file line by line and one that reads the whole thing at once.

In his case, the file that reads line by line only holds one line in memory at a time.

The script that reads the whole file at once is much faster (50%) because it does fewer file reads, but it takes much more memory because it has to hold everything in memory at once.

The consequence is that the fast script fails with fewer concurrent requests than the slow one, because it runs out of memory so quickly.

Lim is just using an example fabricated to make his point, but then gives a lot fo details on tuning your server to get more out of it, then down at the bottom he benchmarks a variety of code optimizations and points out which ones are worth the effort and which ones a inconsequential or just plain PHP myths (like print is faster than echo or some stupid thing like that).

Tom

ergophobe

7:09 pm on Jul 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As a followup, one intriguing thing from the article. He found that "Incrementing an undefined local variable [in a class method] is 9-10 times slower than a pre-initialized one."

This is one more reason to turn up error reporting - you will get warnings for uninitialized variables and, therefore speed up your code according to Lim.