# array_shift() and reindexing

- Thread: https://externals.io/message/61161
- Original archive: https://news-web.php.net/php.internals/61161
- Started by Jille Timmermans on 2012-07-12T11:43:41+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #61161 — Jille Timmermans — 2012-07-12T11:43:41+00:00

URL: https://externals.io/message/61161#61161

Hello,

 `array_shift()` currently reindexes the array after shifting one element. 
The reindexing has quite some impact on it's performance. I would like 
to suggest an extra parameter to `array_shift()` which can be used to 
prevent reindexing.

 From a few quick (and sloppy!) tests I get these results:
   `reset($array); list($key, $value) = each($array); 
unset($array[$key]);` is ~50-150 times faster than `array_shift()`.
   Calling `array_reverse()` and using `array_pop()` is ~2000-5000 times faster.

If you agree this would be an useful addition I will create a patch.

-- Jille

---

## #61162 — David Muir — 2012-07-12T12:39:38+00:00

URL: https://externals.io/message/61161#61162 · In reply to: #61161

What about replacing the existing array with array_slice?

David



> Hello,
> 
> `array_shift()` currently reindexes the array after shifting one element. The reindexing has quite some impact on it's performance. I would like to suggest an extra parameter to `array_shift()` which can be used to prevent reindexing.
> 
> From a few quick (and sloppy!) tests I get these results:
>  `reset($array); list($key, $value) = each($array); unset($array[$key]);` is ~50-150 times faster than `array_shift()`.
>  Calling `array_reverse()` and using `array_pop()` is ~2000-5000 times faster.
> 
> If you agree this would be an useful addition I will create a patch.
> 
> -- Jille

---

## #61163 — Jille Timmermans — 2012-07-12T13:02:11+00:00

URL: https://externals.io/message/61161#61163 · In reply to: #61162

It seems 7-8 times slower than `array_shift()` and that is even without a 
way to fetch the first value.

I tried
   `$array = array_slice($array, 1)`
and
   `$array = array_slice($array, 1, NULL, true);`

-- Jille

Op 12-07-12 14:39, David Muir schreef:
> What about replacing the existing array with array_slice?
>
> David
>
> 
>
> > […]

---

## #61164 — johannes@schlueters.de — 2012-07-12T13:18:54+00:00

URL: https://externals.io/message/61161#61164 · In reply to: #61161

> Hello,
> 
> `array_shift()` currently reindexes the array after shifting one element. 
> The reindexing has quite some impact on it's performance. I would like 
> to suggest an extra parameter to `array_shift()` which can be used to 
> prevent reindexing.
> 
>  From a few quick (and sloppy!) tests I get these results:
>    `reset($array); list($key, $value) = each($array); 
> unset($array[$key]);` is ~50-150 times faster than `array_shift()`.
>    Calling `array_reverse()` and using `array_pop()` is ~2000-5000 times faster.
> 
> If you agree this would be an useful addition I will create a patch.

I haven't checked this, but is the difference only due to the re-hashing
or also due to the fact that that `array_shift()` is taking the array by
reference and therefore making a copy during the call if other
cow-references (refcount>1, isref=0) exist?

johanne

---

## #61165 — Jille Timmermans — 2012-07-12T13:33:22+00:00

URL: https://externals.io/message/61161#61165 · In reply to: #61164

Op 12-07-12 15:18, Johannes Schlüter schreef:
> 
> > […]
> I haven't checked this, but is the difference only due to the re-hashing
> or also due to the fact that that `array_shift()` is taking the array by
> reference and therefore making a copy during the call if other
> cow-references (refcount>1, isref=0) exist?
If I disable the reindexing code `array_shift()` becomes ~300 times faster.

-- Jille
