views:

59

answers:

5

I know many will call this micro optimizing, yes it is that but it is also good to know things like this. Now my question, in my first example below you will see I have a variable set that contains a mysql query then I have another line that runs a function with the above variables mysql query. In my 2nd example I do it all in 1 line, I assume there is no real speed gain but possibly this would help lower memory usage?

//example 1
$sql= 'UPDATE users SET setting_online=0 WHERE user_id=' .$_SESSION['user_id']; 
Database::executeQuery($sql);

//example 2
Database::executeQuery('UPDATE users SET setting_online=0 WHERE user_id=' .$_SESSION['user_id']);
A: 

Have a look at memory_get_usage() and then benchmark it and tell us.

Personally, I'd say it doesn't have any significant impact. The query in your example is quite long though and assigning it to a variable before executing it makes the code somewhat easier to read for me.

Gordon
+2  A: 

Possibly yes, but those 40 or 50 bytes are not going to matter, ever.

But reducing the number of variables is good for an entirely different reason: it reduces the amount of state you as a programmer have to keep track of in order to follow the program - and that makes your job easier and thus the code more maintainable.

Michael Borgwardt
So does using good variables names, though.
tvanfosson
If he's got so many active variables in his code flow that he cannot keep track of them anymore, he's likely got architectural problems. When using objects to encapsulate state and concerns, there should be no need to forego temporary variable assignment. Besides, he could still unset() them by hand when they are no longer needed.
Gordon
+2  A: 

I would make readability/understandability the goal. If using extra variables increases readability, then use them. If removing them improves readability, then remove them.

Also, use parameterized queries everywhere, even when there isn't a chance for SQL injection. It's simply a good habit to get into.

tvanfosson
A: 

yes example 2 will help lower memory usage

Upul
A: 

No matter what the resultant string is stored in memory. Either as something passed to the function, or as a variable, which is then passed into the function.

It largely depends if php stores the string twice once into a variable called sql, and second when passed by value. Since php (more specifically the Zend engine) uses "copy on write" you will not have the string stored twice. It will only be copied when the string is modified.

The only extra memory you will use in the first example then is an extra reference to that copy. This is a minimal addition in memory, so yes the first example will use maybe a couple of extra 32-bit words. This is negligible to the point that if it mattered you probably shouldn't be developing in php. So much is out of your control in terms of memory usage, and if a couple of 32-bit words matters, you should probably be writing your code in C :).

Doug T.