Aditya Kothadiya's Blog

Entrepreneurship, programming, design, productivity, books, philosophy and more.

Archive for the ‘speed’ tag

The PHP Benchmark

Comments

I just stumbled upon a website called PHP Benchmark. It’s a great resource to learn PHP tips for improving performance and speed. These are not advanced tips, but rather very simple tips that we generally don’t pay attention to in our day-to-day programming.

Here is one example -

Is it worth the effort to calculate the length of the loop in advance?

E.g. Should we use


for ($i = 0; $i < $size; $i++)

instead of


for ($i = 0; $i < sizeof($arr); $i++)

The performance result for a loop with 1000 keys with 1 byte values is as below -

With pre calc – count(): Total time: 229 µs
Without pre calc – count(): Total time: 99702 µs
With pre calc – sizeof(): Total time: 235 µs
Without pre calc – sizeof(): Total time: 99610 µs

So it’s very clear that calculating the length of array in advance is way faster than calculating it in the loop.

I hope you’ll find other tips from PHP Benchmark website very useful too.

Written by Aditya

April 18th, 2009 at 10:00 am

Posted in Programming

Tagged with , ,