PDA

View Full Version : Which is faster?



Apothem
07-31-2008, 02:42 AM
Which is faster:
$var = 35;
$num = $var;

OR

$var = $num = 35;

Since I can't seem to tell.

Fou-Lu
07-31-2008, 04:10 AM
I don't have the time to write a test for these right now. I would assume that it would take exactly the same amount of time for either. Since you are dealing with primitive data and simply spreading a copy between two variables, both of the statements are identical.
However (always a little something), this does depend on what the interpreter does. In PHP, I would almost expect that the second is technically faster simply because its a one pass to get two values versus a 2 pass to get two variables. Hard to say without a test. Either way, its negligible.

djm0219
07-31-2008, 10:02 AM
When I see a question like this I'm always curious to know why it really matters.

Any differences, unless they're being used in a loop that's going to be run millions of times, are likely going to be unnoticeable. Which style is used comes down to a matter of personal choice and what a person is comfortable with and used to.

Nightfire
07-31-2008, 11:19 AM
You'll not notice any difference between them. It'll be something like 0.0001 of a second difference between the two.

Fou-Lu
07-31-2008, 09:23 PM
You'll not notice any difference between them. It'll be something like 0.0001 of a second difference between the two.

Yes, I'd question if it would be even that much of a difference with the above.
Really, the things that will optimize you're code the most are correct SQL statements (selects with specific fields, insert/updates with correct datatypes), and loop decomposition. Its true that every little bit helps since PHP is interpreted, but there is a line as to how much is actually necessary. And don't forget that there are two important aspects to optimizing you're code: how long it takes and how much memory it uses. No point in bringing you're time up by 0.000001 seconds if it consumes an additional 500 bytes of memory.