Skip to content

Instantly share code, notes, and snippets.

@ezimuel
Created March 8, 2017 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezimuel/5d9320d4384a23a42a96b180db7747ab to your computer and use it in GitHub Desktop.
Save ezimuel/5d9320d4384a23a42a96b180db7747ab to your computer and use it in GitHub Desktop.
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