0

I read in a few different articles that using array_keys() in foreach loops increases performance, but in practice I really don't notice any increase in speed of my code when using array_keys() on large arrays.

example:

foreach(array_keys($myArray) as $ak){
   $arrayElement =& $myArray[$ak];
   //do whatever I need to do with massive array ...
}
1
  • 5
    This is not the kind of stuff you'll notice, to be honest. Commented Dec 8, 2011 at 16:13

1 Answer 1

1

It is somewhat logical to have some performance gain with large arrays - yes. That way PHP will only copy the array keys in the loop as opposed to keys+values, resulting in a less memory-intensive procedure.

However, as it's been noted in the comments section - it's not something that you'll usually notice. It's deffinately a good practice to make a habit for those things, but you can't actually see it outside of benchmark scores unless you're writing a fairly complex/heavy script.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess you're right. I'm using this on a fairly massive array, so I thought I would see some sort of noticeable performance gain. I do see the timer I threw on the page go down a little bit though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.