Created
March 8, 2017 12:15
-
-
Save ezimuel/5d9320d4384a23a42a96b180db7747ab to your computer and use it in GitHub Desktop.
MD5 vs. preg_replace_callback for PDOStament::bindParam() usage in zend-db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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