Archive for the 'PHP Code' Category

Get geo-cordinates from multiple addresses via google map & google local search

Tuesday, July 3rd, 2007

I recent found a very good article on How to retrieve geo-cordinates of an UK address from Google AJAX Search API. In this article  Tom Anthony explained why google map api doesn’t support UK address geo-coding and how to use Google AJAX Search API to retrieve geo-cordinate of an UK Address. How ever this method is restricted to only one address per request.  If there are more than one addresses, you will have a hard time cracking Google’s AJAX Search API. I found its not possible to make more than two requests to GLocalSearch object since Google has made the search process  an AJAX call.  When the cordinates are returned, there is no way to tell which cordinate is which.

The reason I am searching for a way to retrieve multiple addresses are I am working on an AJAX enabled UK Plumber search site. I will need the cordinates to work how far an address is to the searched postcode.

To meet what I need. I made the following.

Address-ajax-geocode-diagramI created a “caller page”, which I will dump some Javascript calls to retrieve cordinates from an array of addresses and an IFRAME, that contained a modified Tom’s script, so it not only calls to retrieve the cordinate from GLocalSearch, but also a call to return the cordinates to the caller page. When all addresses are retrieved, they will be displayed on the google MAP.

.

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