Over a million developers have joined DZone.

Checking the performance reading arrays with PHP

DZone's Guide to

Checking the performance reading arrays with PHP

· Web Dev Zone
Free Resource

Get deep insight into Node.js applications with real-time metrics, CPU profiling, and heap snapshots with N|Solid from NodeSource. Learn more.

Reading last Fabien Potencier’s post I see something interesting: “ People like micro-optimizations. They are easy to understand, easy to apply… and useless“. It’s true. We don’t need to obsess with small micro-optimizations. As someone said in a comment “ developer time is more precious than processor time“. It’s true again. But normally our code is coded once and executed thousands of times, and we must keep in mind that CPU time many times means money. We need to balance it (as always). But, WTH. I like micro-optimizations, and here comes another one: Checking the access to arrays with PHP.
$limit = 1000000;
$randomKey = rand(0, $limit);

// we create a huge array
$arr = array();
for($i=0; $i<=$limit; $i++) {
    $arr[$i] = $i;

$time = microtime(TRUE);
$mem = memory_get_usage();

$value = $arr[$randomKey];
for($i=0; $i<=$limit; $i++) {
    echo $value;

print_r(array('memory' => (memory_get_usage() - $mem) / (1024 * 1024), 'microtime' => microtime(TRUE) - $time));

with this first example we will loop over a big for statement and we are going to retrieve the value of an array.

[memory] => 5.6837158203125 [microtime] => 0.32066202163696

Now we’re going to do the same, but getting the value of the array inside the loop:

for($i=0; $i<=$limit; $i++) {
    $value = $arr[$randomKey];
    echo $value;

[memory] => 5.6837158203125 [microtime] => 0.35844492912292

and now without creatin the extra variable $value

for($i=0; $i<=$limit; $i++) {
    echo $arr[$randomKey];

[memory] => 5.683666229248 [microtime] => 0.35710000991821


As we can see the memory usage is almost the same, and the time is almost the same too. Obviously fetching the value of the array outside the loop is faster than doing inside the loop. But we must take into account that we’re working with big arrays. With small ones those times are very similar. This time we don’t have any significant outcome. Both methods have the same behaviour. Maybe it can be obvious, but I wanted to check it out.

Node.js application metrics sent directly to any statsd-compliant system. Get N|Solid


Published at DZone with permission of Gonzalo Ayuso, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}