Browse Source

Avoid modulo operation in loop in array_chunk

For this benchmark:
```php
$length = 25;
for ($i=0;$i<1000;$i++)
  array_chunk(range(0, 10000), $length);
```

On an i7-4790, length=25 speeds up by 1.8x and length=1 by 1.27x.
On an i7-1185G7, length=25 speeds up by 1.08x and length=1 by 1.02x.
pull/18980/head
Niels Dossche 3 months ago
parent
commit
4762d46427
  1. 3
      ext/standard/array.c

3
ext/standard/array.c

@ -7046,9 +7046,10 @@ PHP_FUNCTION(array_chunk)
/* If reached the chunk size, add it to the result array, and reset the
* pointer. */
if (!(++current % size)) {
if (++current == size) {
add_next_index_zval(return_value, &chunk);
ZVAL_UNDEF(&chunk);
current = 0;
}
} ZEND_HASH_FOREACH_END();

Loading…
Cancel
Save