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

garabuk
  • 35
  • 6
  • 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. – Tangentially Perpendicular Nov 29 '21 at 08: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. – nice_dev Dec 21 '21 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: https://softwareengineering.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding – miken32 Dec 22 '21 at 21:43

2 Answers2

2

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    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. – Altimus Prime Dec 21 '21 at 15:11
  • In my opinion, that should be like this link https://3v4l.org/3RAU1 There is no big diff and it's up to the PHP version. – garabuk Dec 22 '21 at 20:43
  • @garabuk Your benchmark is flawed. You are not measuring the correct thing. Even if I [fix the bug in your benchmark](https://3v4l.org/bJ43f) it is still not measuring the right thing, but still shows that `array_keys` is faster. – Dharman Dec 22 '21 at 21:11
  • @Dharman I have no objection to that. I just thought mine was a more accurate result. – garabuk Dec 22 '21 at 21:18
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().

nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • 1
    I have to downvote this answer, as it is not very accurate. `array_keys` should be faster by a few magnitutes. – Dharman Dec 21 '21 at 14:53
  • @Dharman That minuscule difference you see is because of function calls that foreach does to the iterator. https://stackoverflow.com/a/14854568/4964822 – nice_dev Dec 21 '21 at 15:15
  • @Dharman I am not sure if this useful, but internally they loop and do the same thing. https://github.com/php/php-src/blob/PHP-5.3/ext/standard/array.c#L2432 – nice_dev Dec 21 '21 at 15:17
  • @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. – nice_dev Dec 21 '21 at 15:18
  • 1
    I don't think it's the iterator methods. See this without `foreach` https://3v4l.org/46ZPj – Dharman Dec 21 '21 at 15:20
  • @Dharman So apparently, `$keys[]` makes it a bit slower but avoiding another loop is better in general in most of the languages. Thanks for this anyway. – nice_dev Dec 21 '21 at 15:29