You are here: Re: Is this a good idea? « PHP Programming Language « IT news, forums, messages
Re: Is this a good idea?

Posted by Ivαn Sαnchez Ortega on 01/18/06 21:05

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chung Leong wrote:

> R. Rajesh Jeba Anbiah wrote:
>>
>> As many people have pointed out, *never* dump the table data into
>> array. Fetch the record and immediately get that processed. If you have
>> any *valid* reason, buffer the data into a very very small (known)
>> sized array. If using MySQL, use the LIMIT if possible.
>
> The reasoning being?

Code refactoring, for example.

As you may already know, refactoring is almost always a good idea, as it
reduces complexity of the algorithm, procesing time, and increases the
cache hit ratio, to name a few consecuences.

Let's suppose the following example:

<?php

mysql_pconnect(blahblahblah);
$r = mysql_query(blahblahblah);
$db_results = array();
while ($row = mysql_fetch_array($r))
{
$db_results[] = $row;
}

foreach ($db_results as $row)
{
foobar;
}

?>

Well, let's refactor that code:


<?php

mysql_pconnect(blahblahblah);
$r = mysql_query(blahblahblah);
while ($row = mysql_fetch_array($r))
{
foobar;
}

?>


Less complexity, less CPU time, less memory, less code. Any developer that
has been taught anything about algorithms knows that. You'd better have a
good reason to not refactorize your code in this way.

> In my opinion conserving memory for the sake of conserving memory is
> just silly. Hardware resources are there to be used. There's nothing
> wrong with a script using a few megs of extra memory, as it'll release
> them a short time later.

That's not a bad idea for batch jobs, but is a terrible one when you have
tenths, hundreds of hits per second. A few MB of memory per script may seem
a small issue, but think about a few MB per script, 100 scripts per second.
A "short time" is not a big thing, but a "short time" hundreds of times per
second is.


- --
- ----------------------------------
IvΓ‘n SΓ‘nchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_eat_s_p_a_m_for_breakfast@hotmail.com
Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFDzpFq3jcQ2mg3Pc8RAr/+AJ9KpKwSANDLvougUKNpuIkSaHK88gCfaliP
h9jM9Tfgy4TmX37P5dNeH3U=
=JSwA
-----END PGP SIGNATURE-----

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация