Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- $str = str_repeat('ウィキ', 99) . 'б';
- function utf8_strrev($str) {
- return iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
- }
- $start = microtime(true);
- for ($i = 0; $i <= 10000; $i++) {
- utf8_strrev($str);
- }
- echo "iconv:\t" . round(microtime(true) - $start, 5) . " s\n";
- function utf8_strrev_mb($str) {
- return mb_convert_encoding(strrev(mb_convert_encoding($str, "UTF-8", "UTF-16BE")), "UTF-16LE", "UTF-8");
- }
- $start = microtime(true);
- for ($i = 0; $i <= 10000; $i++) {
- utf8_strrev_mb($str);
- }
- echo "mb:\t" . round(microtime(true) - $start, 5) . " s\n";
- function utf8_strrev_cycle($str) {
- $out = '';
- for ($l = strlen($str), $i = 0; $i<$l;) {
- $ch = ord($str[$i]);
- $len = ($ch & 0b10000000) ?
- ($ch & 0b11110000) == 0b11110000 ? 4 :
- (
- ($ch & 0b11100000) == 0b11100000 ? 3 : (
- ($ch & 0b11000000) == 0b11000000 ? 2 : 1
- )
- ) : 1;
- $out = substr($str, $i, $len) . $out;
- $i += $len;
- }
- return $out;
- }
- $start = microtime(true);
- for ($i = 0; $i <= 10000; $i++) {
- utf8_strrev_cycle($str);
- }
- echo "cycle:\t" . round(microtime(true) - $start, 5) . " s\n";
- function utf8_rev_another_cycle($text) {
- $result = "";
- for ($index = 0; $index < strlen($text); $index ++)
- {
- $item = $text[$index];
- $byte = ord($item);
- if ($byte < 0x80)
- {
- $result = $item . $result;
- continue;
- }
- if (($byte & 0xc0) == 0x80)
- {
- $sequence .= $item;
- $count --;
- if ($count == 0)
- $result = $sequence . $result;
- continue;
- }
- for ($count = 1; $count <= 6; $count ++)
- {
- $mask = (0xfc0 >> $count) & 0xff;
- $mark = (0xf80 >> $count) & 0xff;
- if (($byte & $mask) == $mark)
- break;
- }
- $sequence = $item;
- }
- return $result;
- }
- $start = microtime(true);
- for ($i = 0; $i <= 10000; $i++) {
- utf8_rev_another_cycle($str);
- }
- echo "an cyc:\t" . round(microtime(true) - $start, 5) . " sec\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement