Advertisement
Guest User

Untitled

a guest
Apr 4th, 2012
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. <?
  2. $str = str_repeat('ウィキ', 99) . 'б';
  3.  
  4. function utf8_strrev($str) {
  5.     return iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
  6. }
  7.  
  8. $start = microtime(true);
  9. for ($i = 0; $i <= 10000; $i++) {
  10.     utf8_strrev($str);
  11. }
  12.  
  13. echo "iconv:\t" . round(microtime(true) - $start, 5) . " s\n";
  14.  
  15.  
  16. function utf8_strrev_mb($str) {
  17.     return mb_convert_encoding(strrev(mb_convert_encoding($str, "UTF-8", "UTF-16BE")), "UTF-16LE", "UTF-8");
  18. }
  19.  
  20. $start = microtime(true);
  21. for ($i = 0; $i <= 10000; $i++) {
  22.     utf8_strrev_mb($str);
  23. }
  24. echo "mb:\t" . round(microtime(true) - $start, 5) . " s\n";
  25.  
  26. function utf8_strrev_cycle($str) {
  27.     $out = '';
  28.  
  29.     for ($l = strlen($str), $i = 0; $i<$l;) {
  30.         $ch = ord($str[$i]);
  31.  
  32.         $len = ($ch & 0b10000000) ?
  33.             ($ch & 0b11110000) == 0b11110000 ? 4 :
  34.             (
  35.                 ($ch & 0b11100000) == 0b11100000 ? 3 : (
  36.                     ($ch & 0b11000000) == 0b11000000 ? 2 : 1
  37.                 )
  38.             ) : 1;
  39.  
  40.         $out = substr($str, $i, $len) . $out;
  41.         $i += $len;
  42.  
  43.     }
  44.     return $out;
  45. }
  46.  
  47. $start = microtime(true);
  48. for ($i = 0; $i <= 10000; $i++) {
  49.     utf8_strrev_cycle($str);
  50. }
  51.  
  52. echo "cycle:\t" . round(microtime(true) - $start, 5) . " s\n";
  53.  
  54. function utf8_rev_another_cycle($text) {
  55.     $result = "";
  56.    for ($index = 0; $index < strlen($text); $index ++)
  57.    {
  58.      $item = $text[$index];
  59.      $byte = ord($item);
  60.      if ($byte < 0x80)
  61.      {
  62.        $result = $item . $result;
  63.        continue;
  64.      }
  65.      if (($byte & 0xc0) == 0x80)
  66.      {
  67.        $sequence .= $item;
  68.        $count --;
  69.        if ($count == 0)
  70.          $result = $sequence . $result;
  71.        continue;
  72.      }
  73.      for ($count = 1; $count <= 6; $count ++)
  74.      {
  75.        $mask = (0xfc0 >> $count) & 0xff;
  76.        $mark = (0xf80 >> $count) & 0xff;
  77.        if (($byte & $mask) == $mark)
  78.          break;
  79.      }
  80.      $sequence = $item;
  81.    }
  82.  
  83.    return $result;
  84. }
  85.  
  86.  
  87. $start = microtime(true);
  88. for ($i = 0; $i <= 10000; $i++) {
  89.     utf8_rev_another_cycle($str);
  90. }
  91.  
  92. echo "an cyc:\t" . round(microtime(true) - $start, 5) . " sec\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement