Friday, October 30, 2009

jQuery-like chaining in PHP's Object Oriented Programming

It's interesting to see how object methods in PHP can be chained just like jQuery's chaining.

jQuery's chaining:

$("div").children(".firstbtn").hide().fadeIn();

In PHP, it is also possible to chain similarly.

Using the string class from Samstyle PHP Framework, we can do chaining in PHP like this:

echo $str->trim()->replace('a', 'b')->value();

To do chaining in PHP, simply add a return statement at the end your methods where appropriate.

return $this;

Advisable to only do this kind of chaining for Action methods, meaning to say that these methods are only doing actions which are not supposed to return value.

Hope this helps!

Monday, October 12, 2009

file_get_contents() vs fread() - Speed Test and Benchmarking

File access is commonly found in any PHP application. Be it caching, or reading data from a file or what. Lately I've been wondering, whether the use of file_get_contents() over fread() is a good practice (i always prefer file_get_contents()).

So this morning I ran a test between this 2 methods of reading a file. The script I used to run is at http://thephpcode.pastebin.com/f2e84fab1

So here's the result from the tests of 5 runs:
 fread() (seconds)file_get_contents() (seconds)
Mean0.036740.01322
Min0.0307109355926510.0098111629486084
Max0.045550823211670.016166925430298


So here it's clear that file_get_contents() is much faster than fread(), mainly because fread() consists of an overhead additional loop and several functions.

Saturday, October 10, 2009

Using Bit Shifting to Divide / Multiply

To think of it, I rarely use bitwise operators in my coding - especially with all the convenience of other operators (can you imagine calculating bits whole day?).

But seriously when I was reading on Bitwise operators, I realised that ($n / 2) == ($n >> 1). So i tested out and...

By shifting bits, we can actually do multiplication and division. But however, this is only true when ($n / $x) is still a integer.


$n = 1600;

var_dump(($n / 2) == ($n >> 1));
var_dump(($n / 4) == ($n >> 2));
var_dump(($n / 8) == ($n >> 3));
var_dump(($n / 16) == ($n >> 4));

var_dump(($n * 2) == ($n << 1));
var_dump(($n * 4) == ($n << 2));
var_dump(($n * 8) == ($n << 3));
var_dump(($n * 16) == ($n << 4));


I ran some tests on which one is faster, and found that bitshifting is actually much faster.

Here are the results:
 Normal (seconds)Bitshift (seconds)
Mean0.124070.10461
Min0.1229791641240.104335069656
Max0.1260280609130.105180025101
Post to the testing script: http://thephpcode.pastebin.com/f1f6ec979

So when you do some simple math in programming, you know ways to speed things up.

Pre vs Post Increment in PHP

Earlier on I was browsing Google Code, and I chanced upon JSpeed - a javascript optimizer. I saw that they actually change post increments to pre-increments. I wondered why and thought I could ask this on Stack Overflow.

One theory came up to be "preincrement (++i) adds one to the value of i, then returns i; in contrast, i++ returns i then adds one to it, which in theory results in the creation of a temporary variable storing the value of i before the increment operation was applied". -- http://physical-thought.blogspot.com/2008/11/pre-vs-post-increment-speed-test.html

So I thought, what about PHP?

I went on to test it on PHP with a FOR loop and I have the following code:
<?php

$time_start = microtime_float();

for($i = 0; $i<10000000;$i++){

}

$time_end = microtime_float();
$loadedin = (float)($time_end - $time_start);
echo $loadedin.' s
';

$time_start = microtime_float();

for($i = 0; $i<10000000;++$i){

}

$time_end = microtime_float();
$loadedin = (float)($time_end - $time_start);
echo $loadedin.' s
';

function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}


?>


Amazingly, pre-increment is much faster than post-increment in PHP. Look at the results:
 Pre-inc (seconds)Post-inc (seconds)
Mean0.686950.74147
Min0.6859259605410.740597963333
Max0.688167810440.743010044098


Thus I conclude that pre-increment is useful in for loops than post-increment. It is much faster through the iterations.