0

I and Jim O'Brien have made two snippets of code, which do exactly the same. But which is faster?

(The code makes key-value pairs of even and odd numbers of the input array. If the input array is this:

Array(
    "thirteen",
    13,
    "seven",
    7,
    ...
)

then the output array will become this:

Array(
    "thirteen" => 13,
    "seven" => 7,
    ...
)

Snippet 1:

<?php

$output = array();
for ($i = 0; $i < count($input); $i++) {
    $output[$input[$i]] = $input[++$i];
}

?>

Snippet 2:

<?php

$output = array();
for ($i = 0; $i < count($input); $i += 2) {
    $output[$input[$i]] = $input[$i + 1];
}

?>

Which snippet is faster, and why?

Community
  • 1
  • 1
MC Emperor
  • 22,334
  • 15
  • 80
  • 130

4 Answers4

7

The difference between them is insignificant compared to what you would save if you pre-computed count($input) instead of running it every time the loop comes around.

Just between the two you have given, the second is more readable and is the one I would choose. Efficiency-wise, it's just not worth bothering about.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
3

This is usually referred to as a micro-optimization. It's not worth worrying about because any gains / differences between the two would be negligible at best. Instead of worrying about things like this, you should focus on readability and following good coding practices. Also, as Kolink pointed out above, you could get the result of count() before going into the loop so that you're not running an unnecessary function call per iteration:

<?php

$output = array();
$count = count($input);
for ($i = 0; $i < $count; $i++) {
    $output[$input[$i]] = $input[++$i];
}

?>
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
1

If PHP compiles this down to assembly instructions, it will probably optimize them to a set of x86 instructions (assuming you're benchmarking for that architecture) that take the same number of CPU cycles.

That said:

  • There is an x86 instruction to increase/decrease a value by one. These will probably be used for both snippets' inner loop command (i.e. ++$i and $i + 1).
  • In the second snippet, the computed result $i + 1 will be thrown away after usage, while
  • the first snippet will keep it. I can only speculate if the generated instructions will write it back to memory (which would be slow) or keep it in a CPU register (which would be fast).
  • Regarding the head of the loop, again snippet one would benefit from the increase-instruction, while snippet two requires an add-instruction. On modern CPUs, both usually take just one CPU cycle. So it doesn't really matter if the second snippet leaves the result computed in the inner loop unused.

In any case, you will only be able to measure a difference when taking a very large number of samples. If you really want to know, you would need a way to look at the generated instructions the PHP compiler produces. As the code is rather small, it shouldn't be too hard to identify the loops and count the CPU cycles by the instructions each snippet produces (see this question for information on how to find the number of cycles per instruction).

Community
  • 1
  • 1
domsom
  • 3,163
  • 1
  • 22
  • 27
0

The difference would be negligible, go for the most readable option.

gamesmad
  • 399
  • 1
  • 2
  • 14