Discussion:
#51122 [NEW]: the iteration on arrays with calling functions is very slow
(too old to reply)
y_kopel at walla dot com
2010-02-23 11:40:36 UTC
Permalink
From: y_kopel at walla dot com
Operating system: linux
PHP version: 5.3.1
PHP Bug Type: Arrays related
Bug description: the iteration on arrays with calling functions is very slow

Description:
------------
the iteration on arrays with calling functions is very slow
comparing php 5.2.1 to 5.3.1

Reproduce code:
---------------
<?php
define("FLOATING_POINT",6);

function ww($f){
echo sprintf("%.".FLOATING_POINT."f", $f)."\n";
}

class A{
protected $users;
function __construct(){
for ($i = 0 ;$i < 100000 ; $i++){
$this->users[$i]['ELEMENT'] = array("a","b");
$this->users[$i]['SUM'] = 2;
}
}

function check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if (count($u['ELEMENT']) > 1){
$sum++;
}
}
return $sum;
}

function a_check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if ($u['SUM'] > 1){
$sum++;
}
}
return $sum;
}
}



$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
}
echo "-----\n";
echo microtime(true) - $start."\n";
$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
}

echo "-----\n";
echo microtime(true) - $start."\n";
?>

Expected result:
----------------
php 5.2.1
=========

-----
-----
1.75261092186
-----
-----
1.05390191078


Actual result:
--------------
php 5.3.1
=========

-----
-----
58.992564916611
-----
-----
30.829360961914
--
Edit bug report at http://bugs.php.net/?id=51122&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51122&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51122&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51122&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=51122&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51122&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=51122&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=51122&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=51122&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=51122&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=51122&r=support
Expected behavior: http://bugs.php.net/fix.php?id=51122&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=51122&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=51122&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=51122&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51122&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=51122&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=51122&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=51122&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=51122&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=51122&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=51122&r=mysqlcfg
j***@php.net
2010-02-23 12:59:03 UTC
Permalink
ID: 51122
Updated by: ***@php.net
Reported By: y_kopel at walla dot com
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: linux
PHP Version: 5.3.1
New Comment:

Just turn off garbage collection if this is a problem for you.
(zend.enable_gc = off in php.ini)


Previous Comments:
------------------------------------------------------------------------

[2010-02-23 11:54:00] y_kopel at walla dot com

shorter code:
<?php

for ($i = 0 ;$i < 1000000 ; $i++){
$users[$i]['SUM'] = 2;
}
$start = microtime(true);
$sum = 0;
foreach ($users as &$u){
if ($u['SUM'] > 1){ $sum++;}
}
echo microtime(true) - $start."\n";
?>

OUTPUT
======

php 5.2.1
=========
0.328261852264

php 5.3.1
=========
42.350708961487

------------------------------------------------------------------------

[2010-02-23 11:40:36] y_kopel at walla dot com

Description:
------------
the iteration on arrays with calling functions is very slow
comparing php 5.2.1 to 5.3.1

Reproduce code:
---------------
<?php
define("FLOATING_POINT",6);

function ww($f){
echo sprintf("%.".FLOATING_POINT."f", $f)."\n";
}

class A{
protected $users;
function __construct(){
for ($i = 0 ;$i < 100000 ; $i++){
$this->users[$i]['ELEMENT'] = array("a","b");
$this->users[$i]['SUM'] = 2;
}
}

function check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if (count($u['ELEMENT']) > 1){
$sum++;
}
}
return $sum;
}

function a_check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if ($u['SUM'] > 1){
$sum++;
}
}
return $sum;
}
}



$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
}
echo "-----\n";
echo microtime(true) - $start."\n";
$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
}

echo "-----\n";
echo microtime(true) - $start."\n";
?>

Expected result:
----------------
php 5.2.1
=========

-----
-----
1.75261092186
-----
-----
1.05390191078


Actual result:
--------------
php 5.3.1
=========

-----
-----
58.992564916611
-----
-----
30.829360961914



------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=51122&edit=1
j***@php.net
2010-02-23 13:02:24 UTC
Permalink
ID: 51122
Updated by: ***@php.net
Reported By: y_kopel at walla dot com
Status: Bogus
Bug Type: Arrays related
Operating System: linux
PHP Version: 5.3.1
New Comment:

Or run the script in 32bit system (and turn of GC :).
With latest SVN checkout of PHP_5_3:

$ src/build/php_5_3/sapi/cli/php -dmemory_limit=1G t.php
1.2259361743927

$ src/build/php_5_3/sapi/cli/php -dmemory_limit=1G -dzend.enable_gc=0
t.php
0.70840787887573

And with latest SVN checkout of PHP_5_2:

$ src/build/php_5_2/sapi/cli/php -dmemory_limit=1G t.php
0.69131684303284

Not noticeable even.


Previous Comments:
------------------------------------------------------------------------

[2010-02-23 12:59:02] ***@php.net

Just turn off garbage collection if this is a problem for you.
(zend.enable_gc = off in php.ini)

------------------------------------------------------------------------

[2010-02-23 11:54:00] y_kopel at walla dot com

shorter code:
<?php

for ($i = 0 ;$i < 1000000 ; $i++){
$users[$i]['SUM'] = 2;
}
$start = microtime(true);
$sum = 0;
foreach ($users as &$u){
if ($u['SUM'] > 1){ $sum++;}
}
echo microtime(true) - $start."\n";
?>

OUTPUT
======

php 5.2.1
=========
0.328261852264

php 5.3.1
=========
42.350708961487

------------------------------------------------------------------------

[2010-02-23 11:40:36] y_kopel at walla dot com

Description:
------------
the iteration on arrays with calling functions is very slow
comparing php 5.2.1 to 5.3.1

Reproduce code:
---------------
<?php
define("FLOATING_POINT",6);

function ww($f){
echo sprintf("%.".FLOATING_POINT."f", $f)."\n";
}

class A{
protected $users;
function __construct(){
for ($i = 0 ;$i < 100000 ; $i++){
$this->users[$i]['ELEMENT'] = array("a","b");
$this->users[$i]['SUM'] = 2;
}
}

function check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if (count($u['ELEMENT']) > 1){
$sum++;
}
}
return $sum;
}

function a_check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if ($u['SUM'] > 1){
$sum++;
}
}
return $sum;
}
}



$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
}
echo "-----\n";
echo microtime(true) - $start."\n";
$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
}

echo "-----\n";
echo microtime(true) - $start."\n";
?>

Expected result:
----------------
php 5.2.1
=========

-----
-----
1.75261092186
-----
-----
1.05390191078


Actual result:
--------------
php 5.3.1
=========

-----
-----
58.992564916611
-----
-----
30.829360961914



------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=51122&edit=1
y_kopel at walla dot com
2010-02-23 11:54:01 UTC
Permalink
ID: 51122
User updated by: y_kopel at walla dot com
Reported By: y_kopel at walla dot com
Status: Open
Bug Type: Arrays related
Operating System: linux
PHP Version: 5.3.1
New Comment:

shorter code:
<?php

for ($i = 0 ;$i < 1000000 ; $i++){
$users[$i]['SUM'] = 2;
}
$start = microtime(true);
$sum = 0;
foreach ($users as &$u){
if ($u['SUM'] > 1){ $sum++;}
}
echo microtime(true) - $start."\n";
?>

OUTPUT
======

php 5.2.1
=========
0.328261852264

php 5.3.1
=========
42.350708961487


Previous Comments:
------------------------------------------------------------------------

[2010-02-23 11:40:36] y_kopel at walla dot com

Description:
------------
the iteration on arrays with calling functions is very slow
comparing php 5.2.1 to 5.3.1

Reproduce code:
---------------
<?php
define("FLOATING_POINT",6);

function ww($f){
echo sprintf("%.".FLOATING_POINT."f", $f)."\n";
}

class A{
protected $users;
function __construct(){
for ($i = 0 ;$i < 100000 ; $i++){
$this->users[$i]['ELEMENT'] = array("a","b");
$this->users[$i]['SUM'] = 2;
}
}

function check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if (count($u['ELEMENT']) > 1){
$sum++;
}
}
return $sum;
}

function a_check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if ($u['SUM'] > 1){
$sum++;
}
}
return $sum;
}
}



$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
}
echo "-----\n";
echo microtime(true) - $start."\n";
$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
}

echo "-----\n";
echo microtime(true) - $start."\n";
?>

Expected result:
----------------
php 5.2.1
=========

-----
-----
1.75261092186
-----
-----
1.05390191078


Actual result:
--------------
php 5.3.1
=========

-----
-----
58.992564916611
-----
-----
30.829360961914



------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=51122&edit=1
y_kopel at walla dot com
2010-02-24 06:57:04 UTC
Permalink
ID: 51122
User updated by: y_kopel at walla dot com
Reported By: y_kopel at walla dot com
Status: Bogus
Bug Type: Arrays related
Operating System: linux
PHP Version: 5.3.1
New Comment:

but...
with and without gc_enable/gc_disable
it consume the same ammount of memory
but work 40 times longer with gc_enable


Previous Comments:
------------------------------------------------------------------------

[2010-02-23 13:02:23] ***@php.net

Or run the script in 32bit system (and turn of GC :).
With latest SVN checkout of PHP_5_3:

$ src/build/php_5_3/sapi/cli/php -dmemory_limit=1G t.php
1.2259361743927

$ src/build/php_5_3/sapi/cli/php -dmemory_limit=1G -dzend.enable_gc=0
t.php
0.70840787887573

And with latest SVN checkout of PHP_5_2:

$ src/build/php_5_2/sapi/cli/php -dmemory_limit=1G t.php
0.69131684303284

Not noticeable even.

------------------------------------------------------------------------

[2010-02-23 12:59:02] ***@php.net

Just turn off garbage collection if this is a problem for you.
(zend.enable_gc = off in php.ini)

------------------------------------------------------------------------

[2010-02-23 11:54:00] y_kopel at walla dot com

shorter code:
<?php

for ($i = 0 ;$i < 1000000 ; $i++){
$users[$i]['SUM'] = 2;
}
$start = microtime(true);
$sum = 0;
foreach ($users as &$u){
if ($u['SUM'] > 1){ $sum++;}
}
echo microtime(true) - $start."\n";
?>

OUTPUT
======

php 5.2.1
=========
0.328261852264

php 5.3.1
=========
42.350708961487

------------------------------------------------------------------------

[2010-02-23 11:40:36] y_kopel at walla dot com

Description:
------------
the iteration on arrays with calling functions is very slow
comparing php 5.2.1 to 5.3.1

Reproduce code:
---------------
<?php
define("FLOATING_POINT",6);

function ww($f){
echo sprintf("%.".FLOATING_POINT."f", $f)."\n";
}

class A{
protected $users;
function __construct(){
for ($i = 0 ;$i < 100000 ; $i++){
$this->users[$i]['ELEMENT'] = array("a","b");
$this->users[$i]['SUM'] = 2;
}
}

function check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if (count($u['ELEMENT']) > 1){
$sum++;
}
}
return $sum;
}

function a_check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if ($u['SUM'] > 1){
$sum++;
}
}
return $sum;
}
}



$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
}
echo "-----\n";
echo microtime(true) - $start."\n";
$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
}

echo "-----\n";
echo microtime(true) - $start."\n";
?>

Expected result:
----------------
php 5.2.1
=========

-----
-----
1.75261092186
-----
-----
1.05390191078


Actual result:
--------------
php 5.3.1
=========

-----
-----
58.992564916611
-----
-----
30.829360961914



------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=51122&edit=1
r***@php.net
2010-02-24 06:59:32 UTC
Permalink
ID: 51122
Updated by: ***@php.net
Reported By: y_kopel at walla dot com
Status: Bogus
Bug Type: Arrays related
Operating System: linux
PHP Version: 5.3.1
New Comment:

With gc on there are gc checks on every iteration. Just because it
doesn't use more memory doesn't mean that gc does add more overhead.


Previous Comments:
------------------------------------------------------------------------

[2010-02-24 06:57:03] y_kopel at walla dot com

but...
with and without gc_enable/gc_disable
it consume the same ammount of memory
but work 40 times longer with gc_enable

------------------------------------------------------------------------

[2010-02-23 13:02:23] ***@php.net

Or run the script in 32bit system (and turn of GC :).
With latest SVN checkout of PHP_5_3:

$ src/build/php_5_3/sapi/cli/php -dmemory_limit=1G t.php
1.2259361743927

$ src/build/php_5_3/sapi/cli/php -dmemory_limit=1G -dzend.enable_gc=0
t.php
0.70840787887573

And with latest SVN checkout of PHP_5_2:

$ src/build/php_5_2/sapi/cli/php -dmemory_limit=1G t.php
0.69131684303284

Not noticeable even.

------------------------------------------------------------------------

[2010-02-23 12:59:02] ***@php.net

Just turn off garbage collection if this is a problem for you.
(zend.enable_gc = off in php.ini)

------------------------------------------------------------------------

[2010-02-23 11:54:00] y_kopel at walla dot com

shorter code:
<?php

for ($i = 0 ;$i < 1000000 ; $i++){
$users[$i]['SUM'] = 2;
}
$start = microtime(true);
$sum = 0;
foreach ($users as &$u){
if ($u['SUM'] > 1){ $sum++;}
}
echo microtime(true) - $start."\n";
?>

OUTPUT
======

php 5.2.1
=========
0.328261852264

php 5.3.1
=========
42.350708961487

------------------------------------------------------------------------

[2010-02-23 11:40:36] y_kopel at walla dot com

Description:
------------
the iteration on arrays with calling functions is very slow
comparing php 5.2.1 to 5.3.1

Reproduce code:
---------------
<?php
define("FLOATING_POINT",6);

function ww($f){
echo sprintf("%.".FLOATING_POINT."f", $f)."\n";
}

class A{
protected $users;
function __construct(){
for ($i = 0 ;$i < 100000 ; $i++){
$this->users[$i]['ELEMENT'] = array("a","b");
$this->users[$i]['SUM'] = 2;
}
}

function check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if (count($u['ELEMENT']) > 1){
$sum++;
}
}
return $sum;
}

function a_check_more_than_one_element(){
$sum = 0;
foreach ($this->users as &$u){
if ($u['SUM'] > 1){
$sum++;
}
}
return $sum;
}
}



$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
$a->check_more_than_one_element();
}
echo "-----\n";
echo microtime(true) - $start."\n";
$a = new A();
$start = microtime(true);
echo "-----\n";
for ($i = 0 ;$i < 5 ; $i++){
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
$a->a_check_more_than_one_element();
}

echo "-----\n";
echo microtime(true) - $start."\n";
?>

Expected result:
----------------
php 5.2.1
=========

-----
-----
1.75261092186
-----
-----
1.05390191078


Actual result:
--------------
php 5.3.1
=========

-----
-----
58.992564916611
-----
-----
30.829360961914



------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=51122&edit=1
y_kopel at walla dot com
2010-02-24 07:19:34 UTC
Permalink
ID: 51122
User updated by: y_kopel at walla dot com
Reported By: y_kopel at walla dot com
Status: Bogus
Bug Type: Arrays related
Operating System: linux
PHP Version: 5.3.1
New Comment:

so... maybe it worth to erite an optimization on it?


Previous Comments:
------------------------------------------------------------------------

[2010-02-24 06:59:31] ***@php.net

With gc on there are gc checks on every iteration. Just because it
doesn't use more memory doesn't mean that gc doesn't add more overhead

------------------------------------------------------------------------

[2010-02-24 06:57:03] y_kopel at walla dot com

but...
with and without gc_enable/gc_disable
it consume the same ammount of memory
but work 40 times longer with gc_enable

------------------------------------------------------------------------

[2010-02-23 13:02:23] ***@php.net

Or run the script in 32bit system (and turn of GC :).
With latest SVN checkout of PHP_5_3:

$ src/build/php_5_3/sapi/cli/php -dmemory_limit=1G t.php
1.2259361743927

$ src/build/php_5_3/sapi/cli/php -dmemory_limit=1G -dzend.enable_gc=0
t.php
0.70840787887573

And with latest SVN checkout of PHP_5_2:

$ src/build/php_5_2/sapi/cli/php -dmemory_limit=1G t.php
0.69131684303284

Not noticeable even.

------------------------------------------------------------------------

[2010-02-23 12:59:02] ***@php.net

Just turn off garbage collection if this is a problem for you.
(zend.enable_gc = off in php.ini)

------------------------------------------------------------------------

[2010-02-23 11:54:00] y_kopel at walla dot com

shorter code:
<?php

for ($i = 0 ;$i < 1000000 ; $i++){
$users[$i]['SUM'] = 2;
}
$start = microtime(true);
$sum = 0;
foreach ($users as &$u){
if ($u['SUM'] > 1){ $sum++;}
}
echo microtime(true) - $start."\n";
?>

OUTPUT
======

php 5.2.1
=========
0.328261852264

php 5.3.1
=========
42.350708961487

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/51122
--
Edit this bug report at http://bugs.php.net/?id=51122&edit=1
Loading...