Posts

Showing posts with the label coding style

For or Foreach? PHP vs. Javascript, C++, Java, HipHop (update: HHVM v2)

Lessons learned: Foreach is 4-5 times faster than For Nested Foreach is 4-5 times faster than nested For Foreach with key lookup is 2 times slower than Foreach without C++ is 60 times faster than PHP running For/Foreach on Arrays Javascript is 2-20 times slower than C++/Java running For on Arrays HipHop is currently no alternative to C++ Using a better CPU makes the code 4-5 times faster Here is a sample script, running on a 1.4 GHz machine with PHP 5.4.0: // init arrays $array = array(); for ($i=0; $i<50000; $i++) $array[] = $i*2; $array2 = array(); for ($i=20000; $i<21000; $i++) $array2[] = $i*2; // test1: foreach big-array (foreach small-array) $start = microtime(true); foreach ($array as $val) { foreach ($array2 as $val2) if ($val == $val2) {} } echo (microtime(true)-$start)."\n"; // test1b: foreach big-array (foreach small-array) $start = microtime(true); foreach ($array as $val) { foreach ($array2 as $val2) if ($val === $val2) {} } echo (microtime(true)-$st...

Decorator or Subclassing?

Using anonymous functions in PHP is very nice to implement a decorator, but what about performance? Results: Subclassing is 40 percent faster than using a decorator Subclassing might require a bit more code Here is the code: class App { public static function route($pattern, $callback, $args) { // evaluate $pattern ... call_user_func_array($callback, $args); } } class AppJson extends App { public static function route($pattern, $callback, $args) { // evaluate $pattern ... $str = json_encode(call_user_func_array($callback, $args)); } } $start = microtime(true); for ($i=0; $i<10000; $i++) { AppJson::route('/json/range', 'range', [0,10]); } echo ' '.(microtime(true)-$start); // 0.1058s $json = function ($func) { return function() use (&$func) { $str = json_encode(call_user_func_array($func, func_get_args())); }; }; $start = microtime(true); for ($i=0; $i<10000; $i++) { App::route('/json/range', $json('ran...

Array key lookup: isset() or array_key_exists() or @ ?

Lessons learned: isset() is faster than array_key_exists() array_key_exists() is faster than @ @ is slower than ignoring notices with error_reporting() Here is the code running on a 1.4 GHz machine with PHP 5.4.0: error_reporting(E_ALL & ~E_NOTICE); $a = array(); for ($i=0; $i<100000; $i++) $a[] = $i*2; $start = microtime(true); for ($i=0; $i<100000; $i++) if (isset($a[$i])) {} echo ' '.(microtime(true)-$start); // 0.017 $start = microtime(true); for ($i=0; $i<100000; $i++) if (array_key_exists($i, $a)) {} echo ' '.(microtime(true)-$start); // 0.064 $start = microtime(true); for ($i=0; $i<100000; $i++) if (@$a[$i]) {} echo ' '.(microtime(true)-$start); // 0.095 $start = microtime(true); for ($i=0; $i<100000; $i++) if ($a[$i]) {} echo ' '.(microtime(true)-$start); // 0.016 $a = array(); $start = microtime(true); for ($i=0; $i<100000; $i++) if (isset($a[$i])) {} echo ' '.(microtime(true)-$start); // 0.016 $start = microtime...

Disadvantages of ORM

ORM has attracted a lot of attention in the last years. So let's get a bit deeper into it. The biggest advantage of ORM is also the biggest disadvantage: queries are generated automatically queries can't be optimized queries select more data than needed, things get slower, more latency (some ORMs fetch all datasets of all relations of an object even though only 1 attribute is read) compiling queries from ORM code is slow (ORM compiler written in PHP) SQL is more powerful than ORM query languages database abstraction forbids vendor specific optimizations Other problems coming up with ORM compiling ORM logic from phpDoc instructions or XML files is slow, but can be cached ORM validates relations and field names outside the database, but can't keep relations consistent ORM libraries are often used in projects without making a benchmark before ORM libraries are often used because the documentation of the library says it is very fast ORM libraries are often used by default with...

Things you should not do in PHP (update: references)

Here is a list of things you should not do in PHP. Most of the stuff is pretty obvious, but over the years I've seen a lot of them. In most cases, these problems remain hidden until data grows above 10000 entries. So on a development system, things are always fast and there are no problems with memory limits :-) Suppose we have a table with 100k entries: $db->query('create table stats (c1 int(11) primary key, c2 varchar(255))'); $db->query('begin'); for ($i=0; $i<100000; $i++) { $db->query('insert into stats values ('.$i.','.($i*2).')'); } $db->query('commit'); Populate a big array instead of streaming results: $result = $db->query('select * from stats'); $array = $result->fetch_all(); // 35M // or while ($row = $result->fetch_assoc()) $array[] = $row; // 35M // or while ($row = $result->fetch_array()) $array[] = $row; // 44.5M // process $array ... // instead of: while ($row = $result->fetch...