RE: [PHP] References and memory efficiency
| From: | Johnson, Kirk | Date: | Mon, 03 Nov 2003 16:14:43 +0000 |
| Subject: | RE: [PHP] References and memory efficiency | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-168399@lists.php.net to get a copy of this message | ||
> I've been working on a database wrapper class for a while
> now, MySQL to
> be specific. Until now, I've simply had a fetch function
> that returned
> an array of all the rows that the database returned, only
> because I have
> gotten so tired of always writing the same while loop to
> iterate through
> all the rows.
>
> However, I have discovered that the method I'm using, passing around
> large multidimensional arrays of text by copy, is extremely memory
> inefficient, so I am working on a new method. Tell me if this
> is any better:
[...snip...]
> Where fetch($query, &$array) is the header. In the second case, the
> fetch function would therefore write the rows directly to the array
> which was passed as a reference rather than returning a copy.
> Am I right
> in thinking that this is a better method?
I hope your new method is the right one, because I do it the same way and
for the same reason :) I haven't done any benchmarks, though. I use
procedural code, but the idea is the same. The calling routine builds the
query string and passes it into a function that executes the query,
processes the results set, puts the results into a hash, and returns a
reference to the hash. The calling routine then gets what it needs out of
the hash. So, one simple scalar gets passed in both directions. I use that
one function for all queries.
Kirk