Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* A benchmark script to compare the speeds of determining time differences using
* PHP and using MySQL.
*
* Copyright 2010 Frederick Ding<br />
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @author Frederick Ding
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/
if (version_compare ( PHP_VERSION, '5.3.0' ) < 0) {
die ( 'This script requires PHP 5.3.' );
}
// set up your MySQL connection here
define ( 'DB_USERNAME', '___' );
define ( 'DB_PASSWORD', '___' );
define ( 'DB_DSN', 'mysql:host=___' );
// how many times will we run the test? (warning: will pound your MySQL server)
define ( 'TEST_ITERATIONS', 10000 );
//
// DON'T EDIT BELOW
//
if (DB_USERNAME == '___' || DB_PASSWORD == '___' || DB_DSN == 'mysql:host=___')
die ( 'You must configure a connection to your database.' );
header ( 'Content-Type: text/plain' );
$utc = new DateTimeZone ( 'UTC' );
define ( 'NOW', gmdate ( 'YmdHis' ) );
/**
* Checks whether the given timestamp is within 15 minutes of the current time.
* @param mixed $timestamp
* @return boolean
*/
function withinFifteenPhp($timestamp) {
global $utc;
$now = new DateTime ( null, $utc );
$given = new DateTime ( $timestamp, $utc );
$interval = $now->diff ( $given, true );
return ($interval->days == 0 && $interval->h == 0 && $interval->i < 15);
}
// create a database connection and the statement to use
// note: we need PDO_MySQL
$db = new PDO ( DB_DSN, DB_USERNAME, DB_PASSWORD );
$query = $db->prepare ( 'SELECT ABS(TIMESTAMPDIFF(MINUTE, ' . NOW . ' , UTC_TIMESTAMP())) < 15' );
// first loop tests PHP
$_timer1 = microtime ( true );
for($i = 0; $i < TEST_ITERATIONS; $i ++) {
withinFifteenPhp ( NOW );
}
$_timer2 = microtime ( true );
// second loop tests MySQL
$_timer3 = microtime ( true );
for($i = 0; $i < TEST_ITERATIONS; $i ++) {
$query->execute ();
// reset so we can reuse this query
$query->closeCursor ();
}
$_timer4 = microtime ( true );
// results
echo 'Average speed (PHP): ' . number_format ( ($_timer2 - $_timer1) / TEST_ITERATIONS, 8 ) . " seconds \n";
echo 'Average speed (MySQL): ' . number_format ( ($_timer4 - $_timer3) / TEST_ITERATIONS, 8 ) . " seconds \n";
if (($_timer2 - $_timer1) > ($_timer4 - $_timer3)) {
// MySQL was faster?
echo 'MySQL was ';
echo ($_timer2 - $_timer1) / ($_timer4 - $_timer3);
echo ' times faster than PHP.';
} else {
// PHP was faster
echo 'PHP was ';
echo ($_timer4 - $_timer3) / ($_timer2 - $_timer1);
echo ' times faster than MySQL.';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment