Created
October 9, 2012 08:27
-
-
Save baranga/3857351 to your computer and use it in GitHub Desktop.
sample implementation of path segment encode with benchmark
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 | |
class PathSegmentEncode | |
{ | |
/** | |
* Map of allowed special chars in path segments. | |
* | |
* http://tools.ietf.org/html/rfc3986#appendix-A | |
* segement = *pchar | |
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | |
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" | |
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | |
* / "*" / "+" / "," / ";" / "=" | |
* | |
* @var array | |
*/ | |
private static $_correctMap = array( | |
'%21' => "!", // sub-delims | |
'%24' => "$", // sub-delims | |
'%26' => "&", // sub-delims | |
'%27' => "'", // sub-delims | |
'%28' => "(", // sub-delims | |
'%29' => ")", // sub-delims | |
'%2A' => "*", // sub-delims | |
// '%2B' => "+", // sub-delims - special value for php/urlencode | |
'%2C' => ",", // sub-delims | |
// '%2D' => "-", // unreserved - not touched by urlencode | |
// '%2E' => ".", // unreserved - not touched by urlencode | |
'%3A' => ":", // pchar | |
'%3B' => ";", // sub-delims | |
'%3D' => "=", // sub-delims | |
'%40' => "@", // pchar | |
// '%5F' => "_", // unreserved - not touched by urlencode | |
'%7E' => "~", // unreserved | |
); | |
public function correct($raw) | |
{ | |
return strtr( | |
urlencode($raw), | |
self::$_correctMap | |
); | |
} | |
public function urlencode($raw) | |
{ | |
return urlencode($raw); | |
} | |
} | |
$encoder = new PathSegmentEncode(); | |
$functions = array( | |
array( | |
'name' => 'urlencode', | |
'call' => array($encoder, 'urlencode'), | |
'time' => 0.0, | |
'rate' => 0.0, | |
), | |
array( | |
'name' => 'PathSegmentEncode::correct', | |
'call' => array($encoder, 'correct'), | |
'time' => 0.0, | |
'rate' => 0.0, | |
), | |
); | |
$testValues = array( | |
'nothing-todo-here', | |
'Hello World!', | |
'!"§$%&/()=?.,;-_\'*+"', | |
'0123456789', | |
'this is some real long test string wich contains all some ugly chars !"§$%&/()=?.,;-_\'*+" 0123456789' | |
); | |
for ($i = 0; $i < 1000; ++$i) { | |
foreach ($testValues as $testValue) { | |
foreach ($functions as $key => $function) { | |
$call = $function['call']; | |
$time = microtime(true); | |
$return = call_user_func($call, $testValue); | |
$time = microtime(true) - $time; | |
$functions[$key]['time'] += $time; | |
} | |
} | |
} | |
$minTime = microtime(true); | |
foreach ($functions as $function) { | |
$minTime = min($function['time'], $minTime); | |
} | |
foreach ($functions as $key => $function) { | |
$functions[$key]['rate'] = $functions[$key]['time'] / $minTime; | |
} | |
foreach ($functions as $function) { | |
printf( | |
"function: %s\n time: %fms\n rate: %f%%\n", | |
$function['name'], | |
$function['time'] * 1000, | |
$function['rate'] * 100 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment