php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13607 Adding to array elements - performance problem
Submitted: 2001-10-09 00:57 UTC Modified: 2001-10-21 02:08 UTC
From: webmaster at vettweb dot net dot au Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.0.5 OS: Win2000 (NT 5.0 build 2195)
Private report: No CVE-ID: None
 [2001-10-09 00:57 UTC] webmaster at vettweb dot net dot au
I have a php script that displays a report.  The report is based on data retrieved from a database (MySQL in this case but can be alomst any).
I am doing some summation on the data retrieved.
I was attempting to add values to the array and found that my script was running fairly slow (30 seconds to process 89 records)
Through alot of trial and error I determined the following problem.
If you have an empty array
i.e. $somearray = array();
and you try to add a value to an element
i.e. $somearray[0] += 100;
it takes considerably longer for php to do this than if we have the following example

$somearray = array(0);
$somearray[0] += 100;

It appears that php has some problems adding to a value in an array if the item does not already exist.

By Initialising my arrays in my script to 0 I cut the execution time for the script from 30 seconds to 4 seconds.  This script was only trying to add into none existant array elements 120 times in my script and yet it took almost 26 seconds to do this.

If this is not a bug it should atleast be brought to peoples attention that this can cause performance problems in some situation.

Here is a complete example script with timer
<?php

function getmicrotime(){
   list($usec, $sec) = explode(" ",microtime());
   return ((float)$usec + (float)$sec);
}

// Create the array
//
$add_test = array();

// Start the first pass timer
//
$firstpassstart = getmicrotime();

// run through the array the first time
//
for ($loopcounter = 0; $loopcounter <= 100; $loopcounter++) {
   // Add 1 to each element
   // Note none of these elements exist
   $add_test[$loopcounter] += 100000;
}

// Stop the first pass timer
//
$firstpassfinish = getmicrotime();
$firstpass = ($firstpassfinish - $firstpassstart);

// Start the Second Pass timer
//
$secondpassstart = getmicrotime();

// run through the array a second time
//
for ($loopcounter = 0; $loopcounter <= 100; $loopcounter++) {
   // Add 1 to each element
   // Note these elements all exist
   $add_test[$loopcounter] += 100000;
}

// Stop the Second Pass timer
//
$secondpassfinish = getmicrotime();
$secondpass = ($secondpassfinish - $secondpassstart);

// Display the results
//
echo "First Pass took  $firstpass seconds<br>\n";
echo "Second Pass took $secondpass seconds<br>\n";

?>

When I run this the first pass is 5 times slower than the second pass.  While the times I get for this script are small (First pass takes 0.004 seconds) in some situations this can be much worse.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-21 02:08 UTC] sniper@php.net
Of course the first pass takes longer.
Add this line in the beginning of the script:

error_reporting(E_ALL);

and you'll understand why. If you have error_reporting
set to 0, it will be faster, but of course it takes
a bit longer to check in the engine whether to throw an
error or not.

--Jani

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 08:01:27 2024 UTC