Forum Moderators: coopster
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!
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
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
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.