Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    Which is faster?

    Which is faster:
    $var = 35;
    $num = $var;

    OR

    $var = $num = 35;

    Since I can't seem to tell.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    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.
    Dave .... HostMonster for all of your hosting needs

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You'll not notice any difference between them. It'll be something like 0.0001 of a second difference between the two.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Nightfire View Post
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •