Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MD5 vs. preg_replace_callback for PDOStament::bindParam() usage in zend-db
<?php
// Testing md5 vs. preg_replace_callback()
$execPreg = 0;
$execMd5 = 0;
for ($i=0; $i<100000; $i++) {
$name = randomName(10);
$start = microtime(true);
$result = preg_replace_callback(
'/([^a-zA-Z0-9_])/',
function ($matches) {
return '_' . ord($matches[0]) . '_';
},
$name
);
$execPreg += microtime(true) - $start;
$start = microtime(true);
$result = md5($name);
$execMd5 += microtime(true) - $start;
}
printf ("Exec time (preg): %f.4\n", $execPreg);
printf ("Exec time (md5) : %f.4\n", $execMd5);
function randomName($size) {
$alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789_$-%&£';
$tot = strlen($alphabet);
$result = '';
for ($i=0; $i<$size; $i++) {
$result .= $alphabet[random_int(0, $tot-1)];
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment