PHP method comparisons

Discussion in 'PHP' started by projectshifter, Jun 1, 2007.

  1. #1
    Fun link I found: http://www.php.lt/benchmark/phpbench.php It compares a lot of different methods such as single vs double quotes, if/else vs switch, and it updates all the values when you load the page so it's actually doing it when you view the page. Interesting read for any PHP gurus or guys who are into high traffic sites. The most interesting thing I found was precalculating the max value in a standard for() loop, if you do for ($i=0; $i<$size; $i++) it's a lot faster having already determined $size vs for ($i=0; $i<count($array); $i++) or sizeOf(). I loaded it up a couple of times and got 6-8x difference. Enjoy!
     
    projectshifter, Jun 1, 2007 IP
  2. CygnetGames

    CygnetGames Peon

    Messages:
    43
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Cool, that's really useful.
     
    CygnetGames, Jun 1, 2007 IP
  3. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Purty cool. Too bad they couldn't have done more comparisons, though.
     
    rgchris, Jun 1, 2007 IP
  4. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What other comparisons did you want to see? I wrote something like this to test switch vs. if/else and stumbled across this article while trying to find more sources, it's not hard to write these, just need to figure out what you want to test.
     
    projectshifter, Jun 1, 2007 IP
  5. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #5
    while vs for,mysql_fetch_array vs mysql_fetch_row??
     
    coderbari, Jun 2, 2007 IP
  6. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #6
    Well, that's a basic programming method you should follow on every language. It's a trivial optimizing technique like using function arguments by reference instead of by copy.
     
    ajsa52, Jun 2, 2007 IP
  7. zonzon

    zonzon Peon

    Messages:
    100
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    pretty good, but nothing ensure that other things (OS, etc...)
    changed the executions times while the bench was running

    but we can also take this in consideration...
     
    zonzon, Jun 2, 2007 IP
  8. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    A lot of that is considered "trivial optimizing", but certain things like predetermining your the max value in a for loop the difference is rather large, and when you're looking at a server with high load and having a large number of visitors, it does matter quite a bit.

    I've got a couple of things I'll be working on all day, but if I find some time I'll definitely try those out. My guess would be that while() is marginally faster and that fetching an array would be almost identical to fetching an array.
     
    projectshifter, Jun 2, 2007 IP
  9. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #9
    mysql_fetch_array() with default type parameter gets an array twice as large as the one returned by mysql_fetch_row() as it gets string and numeric keys for each result. It would be interesting to know what the actual difference between associative and numeric types are as I have assumed that associative results are worth the slight performance hit for the added convenience.
     
    krt, Jun 2, 2007 IP
  10. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    And this is why I made my previous comment =/ Don't say "default type parameter", say "SELECT *", which shouldn't be used for publicly accessible stuff, you should ALWAYS use SELECT col1,col2,col3,colx, otherwise you're grabbing twice as much code, and you're also forcing mysql to run an extra query to grab the names of the columns.
     
    projectshifter, Jun 2, 2007 IP
  11. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #11
    Selecting all columns with SELECT * should not be done in most cases, obviously, but why did you point this out here? If you are confused about me pointing out a "default type parameter", then maybe you were unaware I was referring to the second optional parameter of mysql_fetch_array()
     
    krt, Jun 2, 2007 IP
  12. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I assumed from your comment, had you already known that bit of information you would have also known that in the php man it says that there is no significant speed difference.
     
    projectshifter, Jun 2, 2007 IP