Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:14034 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 76910 invoked by uid 1010); 9 Dec 2004 11:24:58 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 76783 invoked from network); 9 Dec 2004 11:24:58 -0000 Received: from unknown (HELO mail.yhti.net) (66.140.208.18) by pb1.pair.com with SMTP; 9 Dec 2004 11:24:58 -0000 Received: from pc1 (ppp140212162.yhti.net [66.140.212.162]) by mail.yhti.net (8.13.1/8.13.1) with SMTP id iB9BOuEU029075 for ; Thu, 9 Dec 2004 05:24:57 -0600 Message-ID: <002e01c4dde1$b69fe9d0$0100a8c0@pc1> To: Date: Thu, 9 Dec 2004 05:24:56 -0600 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4942.400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4942.400 Subject: Memory needed for arrays? And major performance problems filling an array From: php_lists@realplain.com ("Matt W") Hi all, This is my first time posting to these lists, so I apologize if the internals list is wrong. :-) Before I describe my issue, I had been wondering how I can tell/estimate how much memory PHP will use for arrays? Is there at least a rough formula someone can tell me? My environment: 4.3.6 as an Apache 1.3 module on Windows 2000. OK, now the array performance problem -- I have a simple script that basically just has a 2 dimensional array with this structure: [4-10 character key][int key] = int value. For my examples, the array, $big_arr, has ~40k elements, and ~100k total at the second level. I fill this array in a loop. Then I have this code: $list = implode("','", array_keys($big_arr)); $r = mysql_query(); while ($tmp = mysql_fetch_assoc($r)) { $foo[$tmp['key for string value']] = $tmp; // just 8 small columns in $tmp } The problem is that it takes over 2 seconds to fill the $foo array with ~40k array elements. Ugh! (Still ~1.4s if I use the same 123 scalar value for each.) That's not counting any time to do the query or fetch the rows. I'm thinking it has something to do with $big_arr using a lot of memory (how much??), because a script with just this code: for ($i = 0; $i < 40000; ++$i) { $test["test$i"] = $i; } ... creates 40k elements in 0.3s, *including* the loop's running time. Finally, I tried to unset($big_arr) before filling $foo, but that *increased* running time by ~3.5s! :-( (The unset() took about 0.3s.) Why is that causing $foo to be filled slower?! Is PHP searching for free memory for $foo that had been used by $big_arr? This is disappointing, especially being caused by an array that doesn't seem too outrageously large (and I'd like to go larger). :-( It's going to suck if I can't come up with a way to get it faster... Is there anything that would be different on a version newer than 4.3.6, as I didn't try any yet? Or if it's some Windows thing, then I don't care as much. Comments, suggestions? Thanks! Matt