Sunday, October 21, 2012

PHP Performance Benchmarks

So a friend gave me a link that contains performance benchmarks for PHP.  It's really interesting because you might find a thing or two which you previously believed to be the best way to implement something, when actually it's the worst.  But that's just the worst case.  Still, I think it's pretty useful to know what to avoid, and the alternatives to it.  Here's the link:
        http://maettig.com/code/php/php-performance-benchmarks.php

To summarize, I will list the best implementation in terms of speed for each of the cases from the link above.  Of course, these may be the fastest implementation but they might not exactly be the simplest implementation or the most convenient, but that depends on your needs.

  1. Check if a string is empty
              if (empty($var))     - 3 ms
      X      if ($var == "")    - 44ms

  2. Compare two strings
              $a == $b     - 6 ms

  3. Check for substring (if a string contains another string)
              strpos($haystack, $needle) !== false     - 4 ms
      X      ereg($needle, $haystack)    - 31 ms

  4. Check if a string starts with another string
              strpos($haystack, $needle) === 0    - 4 ms
              strpos($haystack, $needle)    - 4 ms

  5. Check if a string ends with another string
              substr($haystack, -strlen($needle)) === $needle     - 6 ms

  6. Replace a string inside another string
              str_replace($search, $replace, $subject)     - 8 ms
      X      ereg_replace($search, $replace, $subject)     - 41 ms

  7. Trim characters from the beginning and end of a string
              trim($string, ",")     - 1 ms
      X      preg_replace('/^,*|,*$/m', "", $string)     - 47 ms

  8. Split a string into an array
              explode(",", $string)     - 10 ms

  9. Loop a numerical indexed array of strings
              $i = count($array); while($i--)     - 1 ms
              for ($i = 0, $count = count($array); $i < $count; $i++)
                        - 1 ms
      X      for ($i = 0; $i < count($array); $i++)     - 43 ms

  10. Implode an array
              "$array[0] $array[1] $array[2]"     - 5 ms
              implode(" ", $array)     - 5ms

Though differences in execution time between some implementations may be small, I still think it's good to optimize as much as we can.  I know I might be biased on this since I prefer to optimize time over space - actually, having said this, I am indeed biased - but I believe that there is no harm in switching some lines of code, as long as it does what it's supposed to do, only a little faster. :)

Sunday, October 14, 2012

HTML4 to HTML5

At first, I really didn't know what changed from HTML4 to HTML5.  But then of course, HTML5 wouldn't be released if there weren't any improvements from its predecessor.

New Elements.  This probably is the most apparent change from HTML4 to HTML5 for me.  The new elements are added for better webpage structure and semantics, instead of just using div's for every section of your webpage.  Here are some of them:
  • Header
  • Footer
  • Section
  • Article
  • Nav
  • Aside
The difference of these new elements from the simple div?  They have their specific and unique functions that cater just what you need.

Dated Out Styling Tags.  There is a change in the approach of building webpages: HTML provides the structure of the document, and everything else is just presentation.  CSS is responsible for that.  So the tags that focus mainly on the appearance of the webpages have been deprecated in HTML5: <b>, <i>, <font>, etc.  I think this makes a lot of sense, because your webpage will not be tightly-coupled with its style - you can easily modify your design without having to touch the skeleton of your document.

Independence.  I couldn't think of another word, sorry about that, hahaha.  HTML5 can play video and audio files on its own, without the requirement of third-party applications (which I think is pretty cool).  And it can also run video games (yes, video games!) in the browser on its own.


I haven't really explored HTML5 that much, only a couple of experiments with the canvas and video tags (will probably post them soon).  But I find it really interesting, and I believe it has a lot of potential, especially in games (hurrah!).  I hope more browsers will support it soon.


^^