#51122 [Opn]: the iteration on arrays with calling functions is very slow

From: Date: Tue, 23 Feb 2010 11:54:01 +0000
Subject: #51122 [Opn]: the iteration on arrays with calling functions is very slow
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-147979@lists.php.net to get a copy of this message
 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



Thread (7 messages)

« previous php.bugs (#147979) next »