2

I want to get sessionIds array. I know 2 way to fill up and which one should i chooce? Can usage of the array_keys method lose performance?

First way:

//For loop
$aggregateDataPerSessions[$gameSession['game_session_id']] = $gameSession;
$sessionIds[] = $gameSession['game_session_id']
//End loop

Second way:

//For loop
$aggregateDataPerSessions[$gameSession['game_session_id']] = $gameSession;
//End loop
$sessionIds = array_keys($aggregateDataPerSessions);

According to the test; its up to Php version and there is no big diff https://3v4l.org/3RAU1

enter image description here

3
  • 4
    Why don't you just set it up and try it? I suspect that the difference won't be worth worrying about. Beware premature optimisation. Commented Nov 29, 2021 at 8:53
  • 1
    @garabuk Please accept the other answer. Apparently, $sessionIds[] take slightly a bit more of time because of internal dictionary re-indexing it needs to undergo. Commented Dec 21, 2021 at 15:35
  • I think it's worth mentioning that this is likely a pointless optimzation. Sure 0.0003 seconds is 6 times as long as 0.00005 seconds, but unless you are dealing with millions of array elements it's not going to be an appreciable difference. Related reading: softwareengineering.stackexchange.com/questions/99445/… Commented Dec 22, 2021 at 21:43

2 Answers 2

3

Using built-in functions is usually faster than doing the same using a loop. PHP source code is often optimized to handle such operations very efficiently.

array_keys() is more efficient than manually assigning the values in a loop. There are a couple of reasons for this, but the main one is just that array_keys is very fast. Pushing the elements in a loop has the disadvantage of resizing the hash table, assigning new variables and a few other things.

If you can, try to always use built-in functions as they should be magnitudes faster than manual approach.

A non-scientific benchmark

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

4 Comments

This is definitely the right answer (with actual results to prove it). When you run a loop the php interpreter is actually looping through the instructions and interpreting them. The built in functions are actually compiled in the php executable so that they're run at a much lower abstraction level.
In my opinion, that should be like this link 3v4l.org/3RAU1 There is no big diff and it's up to the PHP version.
@garabuk Your benchmark is flawed. You are not measuring the correct thing. Even if I fix the bug in your benchmark it is still not measuring the right thing, but still shows that array_keys is faster.
@Dharman I have no objection to that. I just thought mine was a more accurate result.
0

First method is more efficient than the second one as it avoids another loop to get the keys. However, in terms of asymptotic complexity, they both have same time complexity which is O(n) where n is the size of the array.

All in all, you could use either of the 2 approaches as the difference is pretty negligible. However, to answer the question, first method takes lesser time than the second as it avoids one extra looping that second method does with array_keys().

6 Comments

I have to downvote this answer, as it is not very accurate. array_keys should be faster by a few magnitutes.
@Dharman That minuscule difference you see is because of function calls that foreach does to the iterator. stackoverflow.com/a/14854568/4964822
@Dharman I am not sure if this useful, but internally they loop and do the same thing. github.com/php/php-src/blob/PHP-5.3/ext/standard/array.c#L2432
@Dharman It is ok for using one time I presume as it won't be of much difference, but using it always could lead to increasing runtime. For the scope of this question, using it once is more or less similar.
I don't think it's the iterator methods. See this without foreach 3v4l.org/46ZPj
|

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.