SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 53
  1. #1
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why use variables when not needed?

    Hi

    I have a site which has about 70 users online all the time and is growing fast. I am trying to get the page speeds up and was wondering why people write code with hundreds of variables when they are not even needed.

    Eg why use:
    Code:
    $host   = 'localhost';
    $dbUser = 'x';
    $dbPass = 'x';
    $dbName = 'x';
    
    $conn = @mysql_connect($host, $dbUser, $dbPass);
    if (!$conn) {
     echo 'Unable to connect to the database server at this time.';
     exit();
    }
    
    $choose = @mysql_select_db($dbName);
    if (!$choose) {
     echo 'Error selecting database.';
     exit();
    }
    
    mysql_query($conn);
    mysql_query($choose);
    when you can use:
    Code:
    mysql_connect('localhost','x','x') or die('Critical server error');
    mysql_select_db('x') or die('Critical server error');
    The code is clearly ALOTTTTTT smaller and would be quicker aswell.

    Any thoughts on this issue?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It makes the code more readable and easier to maintain.

  3. #3
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    More readable? I'm the only one using it and I think 2 lines is more readable than 20.

  4. #4
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that the above example is not very representative of the problem. Generally you should not declare variables if they are not needed, however, declaring constants is personal choice. I personally define constants but I don't think that there is any 'correct' way to do it and I don't believe that it will have a serious impact on the performance of the site.

  5. #5
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does make it more readable.

    mysql_connect() takes 4 arguments. Do you know the order for those arguments by hard? Do you know what that 20 integer represents as an argument for some function? You would if either the function call was properly documented (but that seldom happens correctly) or if you stored the vales in descriptively named variables and used those instead.

    Another advantage: you group all settings (variables..) together when you define their values at the top!

    Quaint Tech
    - Blog on web development and web technology.

  6. #6
    SitePoint Evangelist ikeo's Avatar
    Join Date
    Oct 2004
    Location
    Austin Texas
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you overestimate how much quicker your code would be (not much, if any at all). Also in your code, if something went wrong with any of the two lines, you'd know something was wrong ... but how would you know the exact error?

  7. #7
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code is pretty obvious to work out any errors....

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Its all about personal preference at the end of the day. Personall I dont like verbose coding BUT I do like code with lots of comments in it. Variables that aren't used really shouldnt be in the script but it makes very little difference to the overhead (IMHO) if they are there.

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Imagine that you have a script of say about 1000 lines of normal-ish code. Not too minimalistic, not too verbose. But you add roughly the same amount of bytes worth of comments, every line. So your script doubles in size. Does this create any overhead? I've been led to believe script file size is not especially relevant when it comes to performance, but what about the parser having to detect and ignore loads of comments?

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    Toronto
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using variables isn't going to slow down the execution of you program significantly, and in many cases can improve speeds.

    For example, take a for loop
    PHP Code:
    for($x 0$x count($array); $x++) 
    Some people skip putting the result of count($foo_array) into a variable, and instead hardcore it into the for loop. This means that the for loop has to execute the count() function count($foo_array) times, rather than just once if you used:

    PHP Code:
    $foo_count count($foo_array);
    for(
    $x 0$x $foo_count$x++) 

  11. #11
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    8nerd: You have a very good point, and I would like to say that I see people make the above mistake time and time again. It is not a disaster if you do call count every time the loop increments, but in the long run, it will end up slowing down you applications.

  12. #12
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 8nerd View Post
    For example, take a for loop... Some people skip putting the result of count($foo_array) into a variable, and instead hardcore it into the for loop.
    Although your example is valid, even if your array count was in the region of 1000 keys you wouldn't even save half a millisecond.

  13. #13
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman View Post
    Although your example is valid, even if your array count was in the region of 1000 keys you wouldn't even save half a millisecond.
    Also a valid point, but in general I would say that it is good practice to initialize the variable before the loop.

  14. #14
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been led to believe script file size is not especially relevant when it comes to performance, but what about the parser having to detect and ignore loads of comments?
    I just did a quick test on a single script. It was pretty small only 169 lines including the HTML in the document (though it did have includes for DB connection, common functions, authentication etc.)

    Before Adding Comments
    script size - 3354 bytes (excluding includes)
    av. execution time - 26.6 ms

    After Adding a lot of comments, both block and single line
    script size - 6990 bytes (+ 108&#37
    av. execution time - 25.75 ms

    So there you have it, adding comments speeds your scripts up

    However I also tried adding ~4kb of comments to the db connection include to see if including (via the file system) larger files made much of a difference.
    There was a slow down in this case, the avg. execution time went to 34.75 ms (the include went from 2654 to 7008 bytes with comments)

    So I think hugely verbose comments in scripts that get included (most of them if you do OOP) can cause some slow down, but really comments are about developer, not system performance.

    IMO having a 1:1 code to comment ratio would slow the developer more than the parser.

  15. #15
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's true, huge quantities of comments can be quite distracting. That's interesting about the file size mattering with includes. Though it's hardly a difference worth worrying about.

  16. #16
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    Toronto
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    But you add roughly the same amount of bytes worth of comments, every line. So your script doubles in size. Does this create any overhead?
    comments are no real overhead (same with whitespace) - they are skipped over during the lexical analysis stage of compilation/execution

  17. #17
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice thread. I am a very sloppy programmer, I still make a lot of stupid syntax mistakes.

    I love having the variables defined so that when I get an error that I don't understand it is a snap to go in there and throw in a bunch of echos to see what value is being set incorrectly.

  18. #18
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Putting Database connection settings in vars is usually put into a separate file then the one that actually connects. One would do that for when those details change they don't have to hunt for them and they do change maybe not often. Or for users who have no knowledge of your system.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  19. #19
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 8nerd View Post
    comments are no real overhead (same with whitespace) - they are skipped over during the lexical analysis stage of compilation/execution
    This is just not true. There are 3 steps to running a php script: disc access, compile time and execution. Disc access is by far the slowest and reading the file is directly related to the size.

  20. #20
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would personally never remove comments. The tiny performance gain you may get is more than offset against the time you'll spend trying to work out/fix/maintain uncommented code.
    If you're really worried about the performance impact of comments and whitespace then use a PHP code cache which strips comments out of the compiled version.

  21. #21
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999 View Post
    use a PHP code cache
    Can you suggest one?

  22. #22
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Zend Performance Suite or PHP Accelerator are 2 that spring to mind.

  23. #23
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by markl999 View Post
    I would personally never remove comments. The tiny performance gain you may get is more than offset against the time you'll spend trying to work out/fix/maintain uncommented code.
    If you're really worried about the performance impact of comments and whitespace then use a PHP code cache which strips comments out of the compiled version.
    This code cache stripping comments, is that the same as a memcache? Like APC?

  24. #24
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    This code cache stripping comments, is that the same as a memcache? Like APC?
    Yeah, basically anything that caches the PHP opcodes.

  25. #25
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Thats good news! Didnt realise it did that, must look closer at what it does.

    Thanks


Bookmarks

Posting Permissions

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