php對(duì)gb編碼動(dòng)態(tài)轉(zhuǎn)utf-8編碼的幾種方法評(píng)測(cè)
在《IP地址->地理位置轉(zhuǎn)換的測(cè)評(píng)》一文中提到用ip2addr函數(shù)直接讀取IP數(shù)據(jù)庫(kù)文件是效率最高的,相比用MySQL數(shù)據(jù)庫(kù)存儲(chǔ)IP數(shù)據(jù),用SQL查詢是效率最低的。但是IP數(shù)據(jù)庫(kù)文件QQWry.dat是GB2312編碼的。現(xiàn)在我需要UTF-8編碼的地理位置結(jié)果。如果用MySQL方法,可以在數(shù)據(jù)存入數(shù)據(jù)庫(kù)時(shí)就轉(zhuǎn)換為UTF-8編碼,一勞永逸。但是QQWry.dat文件又無法修改,只能把ip2addr函數(shù)的輸出結(jié)果再進(jìn)行動(dòng)態(tài)轉(zhuǎn)換。
動(dòng)態(tài)轉(zhuǎn)換GB->UTF-8編碼至少有四種方法:
用PHP的iconv擴(kuò)展轉(zhuǎn)換
用PHP的mb_string擴(kuò)展轉(zhuǎn)換
用對(duì)換表轉(zhuǎn)換,對(duì)換表存儲(chǔ)在MySQL數(shù)據(jù)庫(kù)中
用對(duì)換表轉(zhuǎn)換,對(duì)換表存儲(chǔ)在文本文件中
前兩種方法要服務(wù)器作了相應(yīng)設(shè)置(編譯安裝了相應(yīng)擴(kuò)展)才能使用。我的虛擬主機(jī)沒有這兩個(gè)擴(kuò)展,只好考慮后兩種方法。前兩個(gè)方法本文也不進(jìn)行測(cè)評(píng)。
測(cè)評(píng)程序如下(func_ip.php參見《IP地址->地理位置轉(zhuǎn)換的測(cè)評(píng)》一文):
<?phprequire_once ('func_ip.php');function u2utf8($c) { $str = ''; if ($c < 0x80) { $str .= $c; } elseif ($c < 0x800) { $str .= chr(0xC0 | $c >> 6); $str .= chr(0x80 | $c & 0x3F); } elseif ($c < 0x10000) { $str .= chr(0xE0 | $c >> 12); $str .= chr(0x80 | $c >> 6 & 0x3F); $str .= chr(0x80 | $c & 0x3F); } elseif ($c < 0x200000) { $str .= chr(0xF0 | $c >> 18); $str .= chr(0x80 | $c >> 12 & 0x3F); $str .= chr(0x80 | $c >> 6 & 0x3F); $str .= chr(0x80 | $c & 0x3F); } return $str;}function GB2UTF8_SQL($strGB) { if (!trim($strGB)) return $strGB; $strRet = ''; $intLen = strlen($strGB); for ($i = 0; $i < $intLen; $i++) { if (ord($strGB{$i}) > 127) { $strCurr = substr($strGB, $i, 2); $intGB = hexdec(bin2hex($strCurr)) - 0x8080; $strSql = 'SELECT code_unicode FROM nnstats_gb_unicode WHERE code_gb = '.$intGB.' LIMIT 1' ; $resResult = mysql_query($strSql); if ($arrCode = mysql_fetch_array($resResult)) $strRet .= u2utf8($arrCode['code_unicode']); else $strRet .= '??'; $i++; } else { $strRet .= $strGB{$i}; } } return $strRet;}function GB2UTF8_FILE($strGB) { if (!trim($strGB)) return $strGB; $arrLines = file('gb_unicode.txt'); foreach ($arrLines as $strLine) { $arrCodeTable[hexdec(substr($strLine, 0, 6))] = hexdec(substr($strLine, 7, 6)); } $strRet = ''; $intLen = strlen($strGB); for ($i = 0; $i < $intLen; $i++) { if (ord($strGB{$i}) > 127) { $strCurr = substr($strGB, $i, 2); $intGB = hexdec(bin2hex($strCurr)) - 0x8080; if ($arrCodeTable[$intGB]) $strRet .= u2utf8($arrCodeTable[$intGB]); else $strRet .= '??'; $i++; } else { $strRet .= $strGB{$i}; } } return $strRet;}function EncodeIp($strDotquadIp) { $arrIpSep = explode('.', $strDotquadIp); if (count($arrIpSep) != 4) return 0; $intIp = 0; foreach ($arrIpSep as $k => $v) $intIp += (int)$v * pow(256, 3 - $k); return $intIp; //return sprintf('%02x%02x%02x%02x', $arrIpSep[0], $arrIpSep[1], $arrIpSep[2], $arrIpSep[3]);}function GetMicroTime() { list($msec, $sec) = explode(' ', microtime()); return ((double)$msec + (double)$sec); }for ($i = 0; $i < 100; $i++) { // 隨機(jī)產(chǎn)生100個(gè)ip地址 $strIp = mt_rand(0, 255).'.'.mt_rand(0, 255).'.'.mt_rand(0, 255).'.'.mt_rand(0, 255); $arrAddr[$i] = ip2addr(EncodeIp($strIp));}$resConn = mysql_connect('localhost', 'netnest', 'netnest');mysql_select_db('test');// 測(cè)評(píng)MySQL查詢的編碼轉(zhuǎn)換$dblTimeStart = GetMicroTime();for ($i = 0; $i < 100; $i++) { $strUTF8Region = GB2UTF8_SQL($arrAddr[$i]['region']); $strUTF8Address = GB2UTF8_SQL($arrAddr[$i]['address']);}$dblTimeDuration = GetMicroTime() - $dblTimeStart;// 測(cè)評(píng)結(jié)束并輸出結(jié)果echo $dblTimeDuration; echo 'rn';// 測(cè)評(píng)文本文件查詢的編碼轉(zhuǎn)換$dblTimeStart = GetMicroTime();for ($i = 0; $i < 100; $i++) { $strUTF8Region = GB2UTF8_FILE($arrAddr[$i]['region']); $strUTF8Address = GB2UTF8_FILE($arrAddr[$i]['address']);}$dblTimeDuration = GetMicroTime() - $dblTimeStart;// 測(cè)評(píng)結(jié)束并輸出結(jié)果echo $dblTimeDuration; echo 'rn';?>測(cè)評(píng)兩次結(jié)果(精確到3位小數(shù),單位是秒):
MySQL查詢轉(zhuǎn)換:0.112文本查詢轉(zhuǎn)換:10.590
MySQL查詢轉(zhuǎn)換:0.099文本查詢轉(zhuǎn)換:10.623
可見這次是MySQL方法遙遙領(lǐng)先于文件查詢法。但是現(xiàn)在還不急于使用MySQL方法,因?yàn)槲谋疚募椒ㄖ匀绱撕臅r(shí),主要因?yàn)樗看无D(zhuǎn)換都要把整個(gè)gb_unicode.txt讀入內(nèi)存,而gb_unicode.txt又是文本文件,格式如下:
0x21210x3000# IDEOGRAPHIC SPACE0x21220x3001# IDEOGRAPHIC COMMA0x21230x3002# IDEOGRAPHIC FULL STOP0x21240x30FB# KATAKANA MIDDLE DOT0x21250x02C9# MODIFIER LETTER MACRON (Mandarin Chinese first tone)……0x552A0x6458# <CJK>0x552B0x658B# <CJK>0x552C0x5B85# <CJK>0x552D0x7A84# <CJK>……0x777B0x9F37# <CJK>0x777C0x9F3D# <CJK>0x777D0x9F3E# <CJK>0x777E0x9F44# <CJK>
文本文件效率較低,于是考慮把文本文件轉(zhuǎn)換為二進(jìn)制文件,然后用折半法查找這個(gè)文件,而不需要把整個(gè)文件讀入內(nèi)存。文件格式為:文件頭2字節(jié),存儲(chǔ)記錄數(shù);接著一條接一條記錄存入文件,每條記錄4字節(jié),前2字節(jié)對(duì)應(yīng)GB代碼,后2字節(jié)對(duì)應(yīng)Unicode代碼。轉(zhuǎn)換程序如下:
<?php$arrLines = file('gb_unicode.txt');foreach ($arrLines as $strLine) { $arrCodeTable[hexdec(substr($strLine, 0, 6))] = hexdec(substr($strLine, 7, 6));}ksort($arrCodeTable);$intCount = count($arrCodeTable);$strCount = chr($intCount % 256) . chr(floor($intCount / 256));$fileGBU = fopen('gbu.dat', 'wb');fwrite($fileGBU, $strCount);foreach ($arrCodeTable as $k => $v) { $strData = chr($k % 256) . chr(floor($k / 256)) . chr($v % 256) . chr(floor($v / 256)); fwrite($fileGBU, $strData);}fclose($fileGBU);?>執(zhí)行程序后就獲得了二進(jìn)制的GB->Unicode對(duì)照表gbu.dat,并且數(shù)據(jù)記錄按GB代碼排了序,便于折半法查找。使用gbu.dat進(jìn)行轉(zhuǎn)碼的函數(shù)如下:
function GB2UTF8_FILE1($strGB) { if (!trim($strGB)) return $strGB; $fileGBU = fopen('gbu.dat', 'rb'); $strBuf = fread($fileGBU, 2); $intCount = ord($strBuf{0}) + 256 * ord($strBuf{1}); $strRet = ''; $intLen = strlen($strGB); for ($i = 0; $i < $intLen; $i++) { if (ord($strGB{$i}) > 127) { $strCurr = substr($strGB, $i, 2); $intGB = hexdec(bin2hex($strCurr)) - 0x8080; $intStart = 1; $intEnd = $intCount; while ($intStart < $intEnd - 1) { // 折半法查找 $intMid = floor(($intStart + $intEnd) / 2); $intOffset = 2 + 4 * ($intMid - 1); fseek($fileGBU, $intOffset); $strBuf = fread($fileGBU, 2); $intCode = ord($strBuf{0}) + 256 * ord($strBuf{1}); if ($intGB == $intCode) { $intStart = $intMid; break; } if ($intGB > $intCode) $intStart = $intMid; else $intEnd = $intMid; } $intOffset = 2 + 4 * ($intStart - 1); fseek($fileGBU, $intOffset); $strBuf = fread($fileGBU, 2); $intCode = ord($strBuf{0}) + 256 * ord($strBuf{1}); if ($intGB == $intCode) { $strBuf = fread($fileGBU, 2); $intCodeU = ord($strBuf{0}) + 256 * ord($strBuf{1}); $strRet .= u2utf8($intCodeU); } else { $strRet .= '??'; } $i++; } else { $strRet .= $strGB{$i}; } } return $strRet;}把其加到原來的測(cè)評(píng)程序,對(duì)三種方法同時(shí)測(cè)評(píng)2次得到數(shù)據(jù)(精確到3位小數(shù),單位:秒):
MySQL方法:0.125文本文件方法:10.873二進(jìn)制文件折半法:0.106
MySQL方法:0.102文本文件方法:10.677二進(jìn)制文件折半法:0.092
可見二進(jìn)制文件折半法還比MySQL法略有優(yōu)勢(shì)。但是上述測(cè)評(píng)都是對(duì)短的地理位置進(jìn)行轉(zhuǎn)碼,如果對(duì)較長(zhǎng)的文本轉(zhuǎn)碼又如何呢?我找來5個(gè)Blog的RSS 2.0文件,都是GB2312編碼。測(cè)評(píng)三種方法對(duì)5個(gè)文件編碼耗費(fèi)的時(shí)間,2次測(cè)量數(shù)據(jù)如下(精確到3位小數(shù),單位:秒):
MySQL方法:7.206文本文件方法:0.772二進(jìn)制文件折半法:5.022
MySQL方法:7.440文本文件方法:0.766二進(jìn)制文件折半法:5.055
可見對(duì)長(zhǎng)的文本是用文本文件的方法最優(yōu),因?yàn)檗D(zhuǎn)碼對(duì)照表讀入內(nèi)存后,轉(zhuǎn)碼就可以很高效了。既然如此,我們還可以嘗試改進(jìn)一下,把文本文件方法改為:轉(zhuǎn)碼對(duì)照表從二進(jìn)制文件gbu.dat讀入內(nèi)存,而不是文本文件。測(cè)評(píng)數(shù)據(jù)如下(精度和單位同上):
從文本文件讀入對(duì)照表:0.766從二進(jìn)制文件讀入對(duì)照表:0.831
從文本文件讀入對(duì)照表:0.774從二進(jìn)制文件讀入對(duì)照表:0.833
表明這次改進(jìn)失敗了,從文本文件讀入轉(zhuǎn)碼對(duì)照表更高效。
總結(jié):用PHP對(duì)GB編碼到UTF-8編碼的動(dòng)態(tài)轉(zhuǎn)換,如果每次轉(zhuǎn)換的文本很小,適宜用二進(jìn)制文件結(jié)合折半法轉(zhuǎn)換;如果每次轉(zhuǎn)換的文本較大,適宜用文本文件存儲(chǔ)轉(zhuǎn)碼對(duì)照表,并在轉(zhuǎn)換前一次性把對(duì)照表讀入內(nèi)存。
