SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 53
  1. #26
    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)
    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.
    Couldn't agree more Mark, where I am currently working the previous developer who wrote a custom OOP CMS (PHP4) didn't comment ANYTHING. Now the class files have around 4000 lines of code - pure code and there are about 10 class files. That equates to c40,000 lines of uncommented code!

    Debuggin that is a PITA and the developer who came after him galantly tried to comment some of it up and explain it but didnt get that far, so coming in cold to 40,000 lines of OOP is painful.

    I would gladly trade a 0.000025 time difference for the 5 months of debugging and function tracing I have had to do
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  2. #27
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't even know how the original author would cope with 40K lines of uncommented code!
    Crazy

  3. #28
    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)
    short answer - he didn't!
    long answer - I think he managed to confuse himself as a few of the functions within the classes are orphaned, variables are referenced through 4 or 5 different classes eg $page variable is located in the user class but accessed via page class => session class => data class => user class.
    So tracking is complex and irritating
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #29
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Of course if you are writing your code properly then the field names and code tells you what it does and the comments remind you of why you coded it that way. Never use comments to describe what the code does because the code already adequately documents that (or at least it does if you write it properly).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #30
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman View Post
    Disc access is by far the slowest and reading the file is directly related to the size.
    Probably not. There's a lot of overhead involved in finding and opening a file, but the actual data-transfer is swift. It doesn't matter a great deal, how large the file is.

    Quote Originally Posted by AlienDev View Post
    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 (...)
    Always measure before you optimize. You can use xdebug + wincachegrind.

  6. #31
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    40000 lines of code....... uncommented..... WOW!!!!!!!! Thats pretty insane and must be a nightmare to understand what half of it even does.

    Thanks whoever made this topic a highlighted discussion! Yay for me :P I noticed it was on the forum index during school and was way happy

  7. #32
    Non-Member I87's Avatar
    Join Date
    Mar 2006
    Location
    UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't worry about what "goes into" your ram
    worry about CPU operations if you want a speed increase


    here's an example of a CPU "hungry" script that doesn't have to be so


    PHP Code:

    <?php
    for($i 1$i <= 50$i++){

    echo 
    'uhh';
    echo (
    5000 344));

    }

    ?>
    it's obvious what's redundant with that script and how it could be improved


    think in a sort of OO way; don't use code more than once that doesn't need to be. Specially in loops

  8. #33
    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)
    Quote Originally Posted by AlienDev View Post
    40000 lines of code....... uncommented..... WOW!!!!!!!! Thats pretty insane and must be a nightmare to understand what half of it even does.
    it was, it is and it probably will be or a few more months!

    Quote Originally Posted by AlienDev
    Thanks whoever made this topic a highlighted discussion! Yay for me :P I noticed it was on the forum index during school and was way happy
    It was turning into quiet a lively discussion and thats what makes good featured threads. It is also applicable to many different languages not just PHP, thats why I recommended it for feature status props to you
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #34
    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)
    Oh, I thought the featured status had something to do with how quickly it was being replied to. I'm glad there's a human element to it.

  10. #35
    is craving 'the potato' slayerment's Avatar
    Join Date
    Nov 2002
    Location
    Scottsdale, Arizona, USA
    Posts
    603
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're the only one handling the code and don't plan to scale the application it really doesn't matter. However, in most cases if multiple people are involved it becomes much more intuitive and scalable with every piece of the code broken down. Breaking things up into variables is part of this process.

  11. #36
    SitePoint Enthusiast jaked409's Avatar
    Join Date
    Jan 2007
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't provide much in the way of statistics, but I can provide some knowledge on the gist of what's going on underneath, to help decide when to use a variable and when not to. This is fairly basic stuff, so many of you will probably know it already, but hopefully it'll help those who don't.

    When you create a variable, it reserves a space in the computer's memory for that variable's value. In example 1 below, you will be using more memory than example 2:

    PHP Code:
    // Example 1
    $loggedIn isLoggedIn();
    if(
    $loggedIn){
      
    // do stuff...
    }

    // Example 2
    if(isLoggedIn()){
      
    // do stuff...

    The amount of memory per variable extremely low (probably never more than a few bytes), but this can add up over a long script spanning multiple functions (each with variables of it's own). Therefore, it's a good idea to not use variables when they don't help anything. That being said, there are three cases when you SHOULD use variables.

    1. When you need one. This one is obvious. Some stuff, you can't do without a variable (like increment a counter, etc.). Don't look for ways around using a variable; as I said, each individual variable adds almost nothing, so you don't have to feel guilty about using them when you need them.

    2. When avoiding one would make something slower. This is similar to what 8nerd said. There are a lot of cases where you end up calling a function over and over again, and the result never changes (like calling count($array) in the head of a for loop). Calling the function once, assigning the answer to a variable, and checking that variable will save the machine from calling the function N times (where N is the number of times the loop happens).

    3. When it makes development easier for you. This one is very important, and depends on personal preference. The original example in this thread illustrates in best:

    Example 1
    PHP 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); 
    Example 2
    PHP Code:
    mysql_connect('localhost','x','x') or die('Critical server error');
    mysql_select_db('x') or die('Critical server error'); 
    While Example 2 will go a bit faster (it uses no variables, while Example 1 uses 6), that "bit" is negligible. What's important here is, which one makes your life easier?

    Some people would prefer Example 1; it's clear what everything is by the variable names, you can change values without looking through the functions, and you can reuse the values in other functions.

    Others would prefer Example 2; it's smaller, so it's easier to glance at and immediately figure out what's happening for many people. Choose whichever one makes your life easier.

    However, if you have no preference (which may start to happen as you use PHP more), pick Example 2. It's a slight bit faster, and if you don't care either way, there's no reason not to pick it.

    One final note: when you're dealing with database resources, ALWAYS assign their results to a variable. If you're running the same database query in every iteration of a loop, you're wasting a massive amount of resources unnecessarily.

  12. #37
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you find yourself inserting the same constant value more than once, you should consider a variable just for the fact that if the value changes later on you don't have to hunt down all the spots you hard coded it.

    The better way (from a maintainability standpoint) would be to not mix these variable out across your code at all - but rather wrap each functional component into a reusable class.

    Lastly for my 2 cents... if you're looking to speed things up on a busy web app, probably the last thing you need to attack is the PHP snippet you've outlined here in the first post. The difference is really negligible.

    Look first into caching and decreasing bandwidth per request.

  13. #38
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what if you're connecting to the database in different php scripts. Wouldn't you want to declare constants in a config file that the mysql_connect would use? What if the password changes or you change servers? Then you have to change the hard coded values in each script.

    I would make variables for everything, that's just the proven way...

  14. #39
    SitePoint Addict Adam A Flynn's Avatar
    Join Date
    Jul 2004
    Location
    Canada
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On the subject of comments, I would always opt for better comments over system performance. There is certainly a point where comments can just be cumbersome (and I think that point depends on the individual developer), but, we're talking about a few milliseconds in the difference here. Buying a stick of RAM for $100 is way cheaper than me wasting a few hours trying to figure my way through poorly documented code at $50/hr.

    As far as the original example goes, in all of my scripts I have a config.php file. At the very least, this file contains constants for DB interaction and other settings. Yes it would be more efficient to have these settings straight in the code itself, but it makes it easier to tweak things later on if all of my config is centralized. Also, it's easier for working with multiple servers if your config (particularly the bits that change based on the working environment) is isolated from the rest of your software.

  15. #40
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ran a quick test to see how much time it takes for storing variables:

    0.00555122 sec. 42 tries without variables.
    0.00557728 sec. 60 tries with variables.

    Differnece: .00002606 sec (0.47&#37

    Without variables:
    PHP Code:
    mysql_connect("localhost","username","password");
    mysql_select_db("databasename");
    mysql_close(); 
    With variables:
    PHP Code:
    $host "localhost";
    $user "username";
    $pass "password";
                
    $connect mysql_connect($host$user$pass);
    $dbname "databasename";
    $db mysql_select_db($dbname);
    mysql_close($connect); 
    Obviously the times would vary from machine to machine, but I think the percentage is probably a pretty good indicator.

    EDIT:

    Also, it's easy to take a middle road here, people.
    While you're working on a project, use all the variables and comments you want. Once the project is complete and about to be published, copy the project to a to-be-published folder, and then take out all unnecessary variables, comments, and whitespace.

  16. #41
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the single best thing to do to speed up your site(assuming it's making a lot of queries) is to optimize your sql and to make sure you're running the absolute best query possible for a given problem

  17. #42
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Creating variables will make the code easier to read, and to seperate code in the correct layout, i.e. the config files should only define variables and constants, but not connect to the database or do any complex things.

    I don't think that creating variables will slow down your application.

    At least a little bit.

    See this testcase:

    PHP Code:
    for ($i 0$i 50000$i ++) {
        ${
    'var' $i} = 'Hello world!';

    This code would create 50000 variables, and it took only 0.1 seconds to do it!

    -

    But in my opinion, I would not use the variable when that data has to be used once only.

    Say, in `mysql_connect`, we specify the host, username and password, but these datas are used only once!
    You need that data just to connect to MySQL database, and nowhere else.

  18. #43
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 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.
    Hello

    comments process in actival server bottlenck only in web sent
    so I thinks no gains server speed are very fast, comments
    good because help understand tricky code sometime logic algoritm is like big books hehe you gets the pics

  19. #44
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by warjockey View Post
    what if you're connecting to the database in different php scripts. Wouldn't you want to declare constants in a config file that the mysql_connect would use? What if the password changes or you change servers? Then you have to change the hard coded values in each script.

    I would make variables for everything, that's just the proven way...
    The connect file is included by php before is excutes any script (using php.ini's prepend_file) so the connect is only ever in one file. I have all my functions and sessions dealt with at the very start of the included file (general.php)

  20. #45
    SitePoint Addict richtestani's Avatar
    Join Date
    Nov 2003
    Location
    Bridgeport
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AlienDev View Post
    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?

    One of the big reasons is reusable code. In the second example - maybe it's easier to read for you or anyone, you'd have to type out the setup everytime it is used.

    Using variables - you only need to change them once. This is big when you want to modify settings withuot having to go through every page to make such changes.
    RichTestani
    -------------------------------
    http://www.junkdepot.com
    http://www.rareoopdvds.com | The Movie Poster Site

  21. #46
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by richtestani View Post
    One of the big reasons is reusable code. In the second example - maybe it's easier to read for you or anyone, you'd have to type out the setup everytime it is used.

    Using variables - you only need to change them once. This is big when you want to modify settings withuot having to go through every page to make such changes.
    Read the post above....

  22. #47
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 8nerd View Post
    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++) 
    Actually, with most modern compilers, that isn't true at all. It will spot that, and automatically add a variable to read the value before it enters the loop - providing it finds that nothing changes $foo_array inside of the loop. So there should be no performance diffrence with a decently written compiler.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  23. #48
    SitePoint Zealot
    Join Date
    Jan 2007
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's considered bad programming to not use variables, and to just plug values in.

    I got dinged on my programming tests in school for doing that.

    If you have a big application, you don't want to be hunting for where to change a value when you can have a config file, or where you can adjust the variables at the beginning of the code. If you move servers you just need to go into the one config file to change the location of your database. Then all your pages pull from the config file instead of you going to each page to change your host, user, password, etc. This goes into OOP and why this is good instead of just creating pages of unmanageable code.

    Also, if you have a variable that is used more than once, you only have to change the variable in one place instead of trying to find all the places where the value needs to be changed. You are more likely to add bugs to your program if you don't use variables.

    Also, it's easier to read for the next programmer to come and change the code. Lets say you grow your business where you can't manage it by yourself. You'll be paying for it in the long run to hire someone to decipher your code if it has numbers/letters/strings set all over the place without using variables.

    This also introduces using good names for variables. Variables are pointless unless they have good descriptive names. If you just plug in values everywhere then you won't know why you chose that value when you come back to it later. Many times your variables won't be values that make much sense if you just look at the value alone.

    If you're looking to speed up your website and it is using a database, look at your queries. This is where a lot of sites have problems loading quickly. Also, look at your database and make sure it has appropriate indexes where needed.

  24. #49
    SitePoint Enthusiast Everah's Avatar
    Join Date
    Mar 2004
    Location
    East Bay Area, CA, USA
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Variables are variable, aren't they? So if there is a potential for the data that the variable will be holding to change, then declare a variable and use it to hold the changing data. If there is no possible chance that the data to be held in the variable to change, but the value of that data could be used extensively throughout the app, declare a variable and assign the static value to it (this is more for flexibility and maintenance) OR use a constant.

    But if you are using a static piece of data that will never change and is used once, use the string for crying out loud. It makes no sense to put that type of information into a variable unless it is config information inside of a portable distributed app.

    This is of course my subject opinion and is, therefore, subject to disagreement and scrutiny.
    Me | My Family | My Design Firm
    Much of what I say can be refuted or disputed.
    I am just a lowly professional PHP developer.
    I could be just as wrong as the next guy.

  25. #50
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    edit: i suck at teh internet


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
  •