Archive for the 'PHP' Category

MD5 with raw_output turned on

Wednesday, April 11th, 2007

On PHP’s MD5() documentation page, tszming at gmail dot com states by calling MD5() with raw_output option turned on and call bin2hex() function to convert it to hexadecimal is 3 times faster than using just MD5() function.

This sounds stupidly impossible, surely by calling one pre-built function should be faster than calling two pre-built functions with same functionality. But tszming is right, calling MD5 along is slower than calling bin2hex(md5()).

This is my test.

First test will create 1000 randomly generated string (ASCII 0 to 250). The test array of strings will be passed to the two functions to measure the time it takes to complete the operation.

Result:

Test 1 Test 2 Test 3 Test 4 Test 5 Average
SimpleMD5 7.05 ms 6.91 ms 6.76 ms 6.74 ms 6.70 ms 6.83 ms
BinMD5 2.49 ms 2.32 ms 2.61 ms 2.45 ms 2.27 ms 2.43 ms

Test Methods:

SimpleMD5 - Convert string to MD5 hash string using MD5 without raw_output option

	foreach ($test_array as $str) {
		$tmp[] = md5($str);
	}

BinMD5 - Convert string to MD5 hash string using tszming’s method, using raw_output option and use bin2hex() to convert them to hexadecimal.

	foreach ($test_array as $str) {
		$tmp[] = bin2hex(md5($str, true));
	}

Full Source Code:

Show Code | Download md5test.txt

Note: Test enviroment is PHP5 + Windows

PHP5 array loop speedtest

Tuesday, April 10th, 2007

While searching for “php benchmark”, I found PHP Benchmark test page. The first benchmark the page showed was foreach(), while(list()=each()) and loop using array_keys(). The test result was stated as “strange”. Loop using array_keys() function out performed the other loop techniques by 2 to 9 folds. Of cause, the test was done in a PHP4 enviroment. Since PHP5 was introduced, there has been a lot of performance increases, the test is out dated.

Eagerly wanting to know how this will perform in PHP5, I have written a set of similar tests to be tested on my PHP5+Windows machine. The result is as expected: completely different, loop using array_keys() is no longer the best performed loop in PHP5.

The test array is made randomly generated 8 characters long string keys and 64 characters long string values.

Test results:

Loop style Test 1 Test 2 Test 3 Test 4 Test 5 Average
ForEachLoop 0.92 0.79 0.70 0.96 0.75 0.82
WhileListEachLoop 2.25 2.10 4.87 2.83 2.11 2.83
ForEachLoopWithKey 1.02 0.98 0.86 0.84 1.03 0.95
WhileListEachLoopWithKey 2.20 2.67 2.13 3.62 2.17 2.56
GetKeyLoop 3.76 6.00 3.30 3.24 4.26 4.11

PHP5 Loop test result

Source code:

Loop 1: foreach() loop without fetching array key

Show Code

  1.   foreach ($test_array as $ele) {
  2.     $tmp[] = $ele;
  3.   }

Loop 2: while(list()=each()) loop without fetching array key

Show Code

  1.   while (list(, $ele) = each($test_array)) {
  2.     $tmp[] = $ele;
  3.   }

Loop 3: foreach() loop fetching array key

Show Code

  1.   foreach ($test_array as $k=>$ele) {
  2.     $tmp[] = $ele;
  3.   }

Loop 4: while(list()=each()) loop fetching array key

Show Code

  1.   while (list($k, $ele) = each($test_array)) {
  2.     $tmp[] = $ele;
  3.   }

Loop 5: loop using array_keys()

Show Code

  1.   $keys = array_keys($test_array);
  2.   $count = sizeOf($keys);
  3.   for ($i=0; $i<$count; $i  ) {
  4.     $tmp[] = $test_array[$keys[$i]];
  5.   }

Full Source:

Show Code | Download looptest.txt

The quicker way to test odd or even

Tuesday, March 27th, 2007

The mathmatically way
Explaination: We all know an even number is multiple of twos, and odd numbers are not. is_float is a PHP function test if $number divided by 2 is a whole number or not.

Show Code

  1.  
  2. echo is_float($number/2);
  3.  

Using bitwise operator
Explaination: In PHP & or AND is a bitwise operator. When used it performs bitwise operation between two numbers. In the following example, the operator will test if the last bit of $number is 1. Thus successfully tests if $number is even or odd.

Show Code

  1.  
  2. echo $number & 1;
  3.  

Conclusion:: Use bitwise operator to test an number is odd or even is much faster than using the division method. More detailed instruction can be found here.