Optimize your PHP code

Some tips based on the Byster.Net Benchmark

Most of us who are coding PHP have some hesitations what to use - “for” instead of “while”, “file()” instead “file_get_contents()”, etc.

I prefer Perl, but when there’s no time, and when the project is coded on PHP, I’m trying to code optimized. Byster.net has posted interesting Benchmark for all that are trying to optimize their code.

And if we have to make some rules from his benchmark, here they are (for PHP5 developers):

  • use for($i=0;$i<10000;$i++) {..} instead of while(list(,$vl = each($arr2)) {..}when reading an array - increases the speed with 2.05 times
  • use for($i=0;$i<10000;$i++) {..} instead of while(list(,$vl = each($arr2)) {..} - increases the speed with 2.05 times
  • use $b = file_get_contents(”t1.log”); insted of $c = file($hdl, $size); - increases the speed with 2.26 times
  • use $b = file_get_contents(”t1.log”); insted of $c = file($hdl, $size); - increases the speed with 2.26 times
  • use preg_match() instead of eregi() - increases the speed with 4.7 times (the preg_match() is using the Perl style regular expressions).
  • use mt_srand() instead of srand() - increases the speed with 5.12 times
  • use PHP4 instead of PHP5 ! - if you want fast site use PHP4 instead of PHP5 !
  • The full benchmark is available here - Byster.Net (http://byster.net/?page_id=48)

    Digg this story | Bookmark this post on del.icio.us

    2 Comments so far
    Leave a comment

    This is pathetic… how about next time putting some thought into a post instead of ripping from another popular source.

    And if you’re gonna make a conclusion… make sure its a good one. e.g. “use PHP4 instead of PHP5″

    Hi, John, thank you for your comment ! This post makes a conclusion from Byster’s benchmark.
    PHP4 is faster than PHP5 fhe functions mentioned above (see the benchmark) and if you don’t need the features of PHP5 is better (for now) to stay on PHP4.
    Yes, PHP5 is faster for Object Oriented code - but this is not the case here…



    Leave a comment
    Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

    (required)

    (required)