PHP 5.2 mysql_connect w/ same parameters does not return a new connection!
This is subtle... I was writing some "reconnect" logic for a DB Class to catch and handle MySQL error code 2006, Server Gone Away. After a solid 5 hours of wasted time I traced the poor logic on my part back to the mysql_connect() function. I was thrown off track at first when the code was hung up on a mysql_select_db() function. Turns out that mysql_connect(), if called twice in an execution series with the same parameters, will take the old link and return that! What The French. So, yes, I read the manual and a fourth param (boolean) can be passed into the function to say I want a NEW connection.
The fourth param is called new_link, and the excerpt from php.net is as follows:
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.
Good to know. Better to remember. Necessary to understand. Fail on my part.
PHP array_diff vs foreach: a battle for speed
1000 runs w/ 1000 data elements in the two arrays (php array diff): 2.7389E-5 seconds
1000 runs w/ 1000 data elements in the two arrays (php foreach): 1.085E-7 seconds
php array diff slower by 2.728E-5 seconds
There were two arrays for this test: $big_set, which had 3147 string elements and $to_diff, which had 1581 string elements. We needed the difference of the two arrays and found that array_diff is slower on average. The nicety to using a foreach(){} is that we can now do other computation within the loop, as opposed to having to loop a second time after the differential set is found. Code below...
<?php print_r($argv); $runs = $argv[2]; echo "$iterations\n"; $data_size = $argv[4]; echo $data_size; $big_set = array(); for($i = 1; $i < $data_size; $i++){ $big_set[] = "Hello there ".rand(500,5000); } $to_diff = array(); for($i = 1; $i < $data_size; $i++){ $to_diff[] = "Hello there ".rand(500,5000); } for ($i=0; $i<$runs; $i++) { $time = 0; $start = microtime(true); $diff = array_diff ($big_set, $to_diff); $t = microtime(true) - $start; $time += $t; } echo $runs, ' runs (php array diff): ', sprintf('%2.9f',$time/$runs), ' secs', BR; for ($i=0; $i<$runs; $i++) { $time2 = 0; $start = microtime(true); foreach($big_set as $p) { unset($to_diff[0]); // cost of an associative array lookup & delete } $t = microtime(true) - $start; $time2 += $t; } echo $runs, ' runs (forloop): ', sprintf('%2.9f',$time2/$runs), ' secs <br />'; if($time2 > $time) { $seconds = $time2 - $time; echo "forloop slower by ". $seconds/$runs ." seconds"; } else { $seconds = $time - $time2; echo "php array diff slower by ". $seconds/$runs ." seconds"; }
Execute via command line :/>php file.php -runs
Must be in that order -- I did not make it handle fancy arguments.
Update: Adding PHP Bench for other cool benchmarks



