Skip to content

Instantly share code, notes, and snippets.

@s3b4stian
Created December 9, 2017 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s3b4stian/2e31fe85b45f03eb1a016fd74e12f336 to your computer and use it in GitHub Desktop.
Save s3b4stian/2e31fe85b45f03eb1a016fd74e12f336 to your computer and use it in GitHub Desktop.
ordutf8 refactor spet test
<?php
function ordutf8_step1(string $char) : int
{
$code = ord(substr($char, 0, 1));
if ($code >= 128) {
if ($code < 224)
$bytesnumber = 2;
else if ($code < 240)
$bytesnumber = 3;
else if ($code < 248)
$bytesnumber = 4;
$codetemp = $code - 192 - ($bytesnumber > 2 ? 32 : 0) - ($bytesnumber > 3 ? 16 : 0);
$offset = 0;
for ($i = 2; $i <= $bytesnumber; $i++) {
$offset ++;
$code2 = ord(substr($char, $offset, 1)) - 128;
$codetemp = $codetemp * 64 + $code2;
}
$code = $codetemp;
}
return $code;
}
function ordutf8_step2(string $char) : int
{
$code = ord(substr($char, 0, 1));
if ($code >= 128) {
$count = 0;
if ($code < 224) {
$bytesnumber = 2;
} else if ($code < 240) {
$bytesnumber = 3;
$count = 32;
} else if ($code < 248) {
$bytesnumber = 4;
$count = 48;
}
$codetemp = $code - 192 - $count;
$offset = 0;
for ($i = 2; $i <= $bytesnumber; $i++) {
$offset ++;
$code2 = ord(substr($char, $offset, 1)) - 128;
$codetemp = $codetemp * 64 + $code2;
}
$code = $codetemp;
}
return $code;
}
function ordutf8_step3(string $char) : int
{
$code = ord(substr($char, 0, 1));
if ($code >= 128) {
$count = 0;
if ($code < 224) {
$bytes = 2;
} else if ($code < 240) {
$bytes = 3;
$count = 32;
} else if ($code < 248) {
$bytes = 4;
$count = 48;
}
$temp = $code - 192 - $count;
for ($i = 1; $i < $bytes; $i++) {
$code = $temp = $temp * 64 + ord(substr($char, $i, 1)) - 128;
}
}
return $code;
}
function ordutf8_step4(string $char) : int
{
$code = ord(substr($char, 0, 1));
if ($code > 127) {
$bytes = 2;
$count = 0;
if ($code > 223){
$bytes = 3;
$count = 32;
}
if ($code > 239){
$bytes = 4;
$count = 48;
}
$temp = $code - 192 - $count;
for ($i = 1; $i < $bytes; $i++) {
$code = $temp = $temp * 64 + ord(substr($char, $i, 1)) - 128;
}
}
return $code;
}
function ordutf8_step5(string $char) : int
{
$code = ord(substr($char, 0, 1));
if ($code > 239){
return ((($code - 240) * 64 + ord(substr($char, 1, 1)) - 128) *
64 + ord(substr($char, 2, 1)) - 128) *
64 + ord(substr($char, 3, 1)) - 128;
}
if ($code > 223){
return (($code - 224) * 64 + ord(substr($char, 1, 1)) - 128)
* 64 + ord(substr($char, 2, 1)) - 128;
}
if ($code > 127) {
return ($code - 192) * 64 + ord(substr($char, 1, 1)) - 128;
}
return $code;
}
$cicles = 10000;
$char = 'ݟ';
$time = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
ordutf8_step1($char);
}
$time += microtime(true);
echo $time;
echo "<br/>\n";
$time = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
ordutf8_step2($char);
}
$time += microtime(true);
echo $time;
echo "<br/>\n";
$time = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
ordutf8_step3($char);
}
$time += microtime(true);
echo $time;
echo "<br/>\n";
$time = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
ordutf8_step4($char);
}
$time += microtime(true);
echo $time;
echo "<br/>\n";
$time = -microtime(true);
for($i = 0; $i < $cicles; $i++) {
ordutf8_step5($char);
}
$time += microtime(true);
echo $time;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment