1
$fail_row = mysql_fetch_array($sql_fail_check);
$tries = $time_row['tries'];

I was reading that it saves memory to do thing inline/less lines. Since i am only going to need the tries count from that MySql array only, could i make this be just one line somehow?

Keverw
  • 3,736
  • 7
  • 31
  • 54
  • If you really only SELECT one column, then you *could* (= not *should*) compact it into `$tries = current(mysql_fetch_array($res));`. But that's really just a compacter notation. It saves no measurable amount of memory or anything. – mario Apr 27 '11 at 22:49
  • @mario: `current()` needs an actual variable (`$something`) as a parameter AFAIK, not an evaluated expression (a function call). – soulmerge Apr 27 '11 at 22:50
  • @mario: I'm very surprised it doesn't, the docs say it requires a reference (http://php.net/current), whereas the cli-test works (`php -r 'var_dump(current(array(1)));'`) Good to know, though, thx! – soulmerge Apr 27 '11 at 22:56

4 Answers4

5

Forget that piece of advice - it is utter, complete nonsense.

Re your update: The advice you quote is suggesting compressing

$description = strip_tags($_POST['description']);
echo $description;

into

echo strip_tags($_POST['description']);

this makes sense for very large amounts of data, because the step of storing the value in a variable ($description) doubles the amount of memory needed to store description, which could lead memory limit problems.

In your case however,

  • There is no elegant way to directly address an element from an array coming from a function call (like $tries = mysql_fetch_array($sql_fail_check)["tries"];) - it's syntactically not possible.

  • tries is probably never going to be large enough for this optimization to make any difference.

Remember: Optimize only when necessary, or if you know that you are going to be dealign with really significant amounts of data. Unless this is the case, code readability always comes first.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Really? I was reading it at http://code.google.com/speed/articles/optimizing-php.html "You can simply do this operation inline and avoid the extra memory consumption" So i was thinking if i rewrote this to be one line, it would be a little faster. – Keverw Apr 27 '11 at 22:47
  • `SELECT tries FROM failedauth WHERE ip='$binip'` is the SQL i have. So i know the SQL is optimized. just trying to optimize the php to go along with it. – Keverw Apr 27 '11 at 22:48
  • 1
    Google tend to micro-optimised due to the sheer amount of traffic they get. A couple of kilobytes saved per request by Google will save them loads in bandwidth on a monthly basis, but for average-sized sites the gains are not noticeable. – Martin Bean Apr 27 '11 at 22:49
  • 2
    Less memory consumption != faster. In fact, it's frequently the case that you can get better performance if you're willing to accept higher memory consumption. – Matt Ball Apr 27 '11 at 22:49
  • 1
    @Kever There's something to that: In the article you mention, a variable is filled with a (potentially large) value. The optimization is to skip the step where the variable gets saved, which is fundamentally a good idea. However, there is no elegant direct way to address an element that comes as the result of a function call (like `echo mysql_fetch_array($sql_fail_check)["tries"];` , so the way you have is already the most direct way to access the element. It doesn't matter - I assume `tries` is never going to be megabytes large, so the amount of memory wasted by this is minuscule. – Pekka Apr 27 '11 at 22:50
  • Okay. Thanks for explaining that. tries is just a number of failed logins per ip. I guess i'm over worrying about making a efficient app. – Keverw Apr 27 '11 at 22:57
  • @Kever yeah, there is absolutely no need to optimize that. Code readability is much more important. – Pekka Apr 27 '11 at 23:00
3

Actually , if i understand this right, then: doing $a = 1; $b = $a; will not use twice as much memory, because it will just mean that $a and $b both point to same address in memory.

Then new space is allocated only if you now change either $a or $b.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Is this how it works in PHP internally? Are you sure? It would be new to me. – Pekka Apr 27 '11 at 23:10
  • 1
    @Pekka: Yes, this is how it works. See my answer here: http://stackoverflow.com/questions/5794746/smarty-and-other-tpl-ngins-assign-and-assign-by-ref/5794821#5794821 – NikiC Apr 27 '11 at 23:14
  • @nikic fair enough, thanks! +1 then for this, this is relevant to the question. – Pekka Apr 27 '11 at 23:18
2

You could do this:

$tries = mysql_fetch_object($sql_fail_check)->tries;
1

Yes, you can combine both lines. Given tries is the first SELECTed column you could for example do:

list($tries) = mysql_fetch_num($result);

See this answer for more info on Array Dereferencing (this is what they call what you're trying to do).

PS: Just another reason to use PDO, where you just write:

$tries = $pdo->query('SELECT tries FROM table')->fetchColumn(0);
Community
  • 1
  • 1
NikiC
  • 100,734
  • 37
  • 191
  • 225