首頁  >  文章  >  後端開發  >  优化PHP in_array()函数,效率提高50倍

优化PHP in_array()函数,效率提高50倍

WBOY
WBOY原創
2016-06-20 13:02:501662瀏覽

/原始程序
function pinyin($str) { //判断是否是 单拼  双拼 三拼 四拼
    
    $pin_arr = array("a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian", "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "cao", "che", "chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "chuan", "cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "dui", "dong", "dou", "du", "duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang", "gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang", "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian", "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "kei", "ken", "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng", "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai", "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai", "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan", "nue", "nuo", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pou", "pu", "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", "re", "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng", "sha", "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", "shui", "shun", "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi", "xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yong", "yao", "ye", "yi", "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha", "zhai", "zhan", "zhong", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo");
   
    if (in_array($str, $pin_arr)) {
        return 1;
    }
    
    for ($i = 1; $i < strlen($str); $i++) {
 
        if ($i < 7) {
            if (pinyin(substr($str, 0, $i)) && $n = pinyin(substr($str, $i))) {
 
                return $n + 1;
            }
        } else {
            return 0;
        }
    }
    return 0;
}

上面$pin_arr是个大数组,大概400个元素,从上可以看出单拼域名、双拼域名、三拼域名概念:如果一个域名【这里$str是指域名的中间部分,如www.scutephp.com则是scutephp】全部由$pin_arr中n个元素组成,则是n拼域名。
看看优化后的程序代码:

/**
 * 优化后的判断n拼域名函数
 * 
 */
$pin_arr =array(&#39;a&#39;=>&#39;&#39;,&#39;ai&#39;=>&#39;&#39;,&#39;an&#39;=>&#39;&#39;,&#39;ang&#39;=>&#39;&#39;,&#39;ao&#39;=>&#39;&#39;,&#39;ba&#39;=>&#39;&#39;,&#39;bai&#39;=>&#39;&#39;,&#39;ban&#39;=>&#39;&#39;,&#39;bang&#39;=>&#39;&#39;,&#39;bao&#39;=>&#39;&#39;,&#39;bei&#39;=>&#39;&#39;,&#39;ben&#39;=>&#39;&#39;,&#39;beng&#39;=>&#39;&#39;,&#39;bi&#39;=>&#39;&#39;,&#39;bian&#39;=>&#39;&#39;,&#39;biao&#39;=>&#39;&#39;,&#39;bie&#39;=>&#39;&#39;,&#39;bin&#39;=>&#39;&#39;,&#39;bing&#39;=>&#39;&#39;,&#39;bo&#39;=>&#39;&#39;,&#39;bu&#39;=>&#39;&#39;,&#39;ca&#39;=>&#39;&#39;,&#39;cai&#39;=>&#39;&#39;,&#39;can&#39;=>&#39;&#39;,&#39;cang&#39;=>&#39;&#39;,&#39;cao&#39;=>&#39;&#39;,&#39;ce&#39;=>&#39;&#39;,&#39;ceng&#39;=>&#39;&#39;,&#39;cha&#39;=>&#39;&#39;,&#39;chai&#39;=>&#39;&#39;,&#39;chan&#39;=>&#39;&#39;,&#39;chang&#39;=>&#39;&#39;,&#39;cao&#39;=>&#39;&#39;,&#39;che&#39;=>&#39;&#39;,&#39;chen&#39;=>&#39;&#39;,&#39;cheng&#39;=>&#39;&#39;,&#39;chi&#39;=>&#39;&#39;,&#39;chong&#39;=>&#39;&#39;,&#39;chou&#39;=>&#39;&#39;,&#39;chu&#39;=>&#39;&#39;,&#39;chuai&#39;=>&#39;&#39;,&#39;chuan&#39;=>&#39;&#39;,&#39;chuang&#39;=>&#39;&#39;,&#39;chui&#39;=>&#39;&#39;,&#39;chun&#39;=>&#39;&#39;,&#39;chuo&#39;=>&#39;&#39;,&#39;ci&#39;=>&#39;&#39;,&#39;cong&#39;=>&#39;&#39;,&#39;cou&#39;=>&#39;&#39;,&#39;cu&#39;=>&#39;&#39;,&#39;chuan&#39;=>&#39;&#39;,&#39;cui&#39;=>&#39;&#39;,&#39;cun&#39;=>&#39;&#39;,&#39;cuo&#39;=>&#39;&#39;,&#39;da&#39;=>&#39;&#39;,&#39;dai&#39;=>&#39;&#39;,&#39;dan&#39;=>&#39;&#39;,&#39;dang&#39;=>&#39;&#39;,&#39;dao&#39;=>&#39;&#39;,&#39;de&#39;=>&#39;&#39;,&#39;deng&#39;=>&#39;&#39;,&#39;di&#39;=>&#39;&#39;,&#39;dian&#39;=>&#39;&#39;,&#39;diao&#39;=>&#39;&#39;,&#39;die&#39;=>&#39;&#39;,&#39;ding&#39;=>&#39;&#39;,&#39;dui&#39;=>&#39;&#39;,&#39;dong&#39;=>&#39;&#39;,&#39;dou&#39;=>&#39;&#39;,&#39;du&#39;=>&#39;&#39;,&#39;duan&#39;=>&#39;&#39;,&#39;dui&#39;=>&#39;&#39;,&#39;dun&#39;=>&#39;&#39;,&#39;duo&#39;=>&#39;&#39;,&#39;e&#39;=>&#39;&#39;,&#39;en&#39;=>&#39;&#39;,&#39;er&#39;=>&#39;&#39;,&#39;fa&#39;=>&#39;&#39;,&#39;fan&#39;=>&#39;&#39;,&#39;fang&#39;=>&#39;&#39;,&#39;fei&#39;=>&#39;&#39;,&#39;fen&#39;=>&#39;&#39;,&#39;feng&#39;=>&#39;&#39;,&#39;fo&#39;=>&#39;&#39;,&#39;fou&#39;=>&#39;&#39;,&#39;fu&#39;=>&#39;&#39;,&#39;ga&#39;=>&#39;&#39;,&#39;gai&#39;=>&#39;&#39;,&#39;gan&#39;=>&#39;&#39;,&#39;gang&#39;=>&#39;&#39;,&#39;gao&#39;=>&#39;&#39;,&#39;ge&#39;=>&#39;&#39;,&#39;gei&#39;=>&#39;&#39;,&#39;gen&#39;=>&#39;&#39;,&#39;geng&#39;=>&#39;&#39;,&#39;gong&#39;=>&#39;&#39;,&#39;gou&#39;=>&#39;&#39;,&#39;gu&#39;=>&#39;&#39;,&#39;gua&#39;=>&#39;&#39;,&#39;guai&#39;=>&#39;&#39;,&#39;guan&#39;=>&#39;&#39;,&#39;guang&#39;=>&#39;&#39;,&#39;gui&#39;=>&#39;&#39;,&#39;gun&#39;=>&#39;&#39;,&#39;guo&#39;=>&#39;&#39;,&#39;ha&#39;=>&#39;&#39;,&#39;hai&#39;=>&#39;&#39;,&#39;han&#39;=>&#39;&#39;,&#39;hang&#39;=>&#39;&#39;,&#39;hao&#39;=>&#39;&#39;,&#39;he&#39;=>&#39;&#39;,&#39;hei&#39;=>&#39;&#39;,&#39;hen&#39;=>&#39;&#39;,&#39;heng&#39;=>&#39;&#39;,&#39;hong&#39;=>&#39;&#39;,&#39;hou&#39;=>&#39;&#39;,&#39;hu&#39;=>&#39;&#39;,&#39;hua&#39;=>&#39;&#39;,&#39;huai&#39;=>&#39;&#39;,&#39;huan&#39;=>&#39;&#39;,&#39;huang&#39;=>&#39;&#39;,&#39;hui&#39;=>&#39;&#39;,&#39;hun&#39;=>&#39;&#39;,&#39;huo&#39;=>&#39;&#39;,&#39;ji&#39;=>&#39;&#39;,&#39;jia&#39;=>&#39;&#39;,&#39;jian&#39;=>&#39;&#39;,&#39;jiang&#39;=>&#39;&#39;,&#39;jiao&#39;=>&#39;&#39;,&#39;jie&#39;=>&#39;&#39;,&#39;jin&#39;=>&#39;&#39;,&#39;jing&#39;=>&#39;&#39;,&#39;jiong&#39;=>&#39;&#39;,&#39;jiu&#39;=>&#39;&#39;,&#39;ju&#39;=>&#39;&#39;,&#39;juan&#39;=>&#39;&#39;,&#39;jue&#39;=>&#39;&#39;,&#39;jun&#39;=>&#39;&#39;,&#39;ka&#39;=>&#39;&#39;,&#39;kai&#39;=>&#39;&#39;,&#39;kan&#39;=>&#39;&#39;,&#39;kang&#39;=>&#39;&#39;,&#39;kao&#39;=>&#39;&#39;,&#39;ke&#39;=>&#39;&#39;,&#39;kei&#39;=>&#39;&#39;,&#39;ken&#39;=>&#39;&#39;,&#39;keng&#39;=>&#39;&#39;,&#39;kong&#39;=>&#39;&#39;,&#39;kou&#39;=>&#39;&#39;,&#39;ku&#39;=>&#39;&#39;,&#39;kua&#39;=>&#39;&#39;,&#39;kuai&#39;=>&#39;&#39;,&#39;kuan&#39;=>&#39;&#39;,&#39;kuang&#39;=>&#39;&#39;,&#39;kui&#39;=>&#39;&#39;,&#39;kun&#39;=>&#39;&#39;,&#39;kuo&#39;=>&#39;&#39;,&#39;la&#39;=>&#39;&#39;,&#39;lai&#39;=>&#39;&#39;,&#39;lan&#39;=>&#39;&#39;,&#39;lang&#39;=>&#39;&#39;,&#39;lao&#39;=>&#39;&#39;,&#39;le&#39;=>&#39;&#39;,&#39;lei&#39;=>&#39;&#39;,&#39;leng&#39;=>&#39;&#39;,&#39;li&#39;=>&#39;&#39;,&#39;lia&#39;=>&#39;&#39;,&#39;lian&#39;=>&#39;&#39;,&#39;liang&#39;=>&#39;&#39;,&#39;liao&#39;=>&#39;&#39;,&#39;lie&#39;=>&#39;&#39;,&#39;lin&#39;=>&#39;&#39;,&#39;ling&#39;=>&#39;&#39;,&#39;liu&#39;=>&#39;&#39;,&#39;long&#39;=>&#39;&#39;,&#39;lou&#39;=>&#39;&#39;,&#39;lu&#39;=>&#39;&#39;,&#39;lv&#39;=>&#39;&#39;,&#39;luan&#39;=>&#39;&#39;,&#39;lue&#39;=>&#39;&#39;,&#39;lun&#39;=>&#39;&#39;,&#39;luo&#39;=>&#39;&#39;,&#39;ma&#39;=>&#39;&#39;,&#39;mai&#39;=>&#39;&#39;,&#39;man&#39;=>&#39;&#39;,&#39;mang&#39;=>&#39;&#39;,&#39;mao&#39;=>&#39;&#39;,&#39;me&#39;=>&#39;&#39;,&#39;mei&#39;=>&#39;&#39;,&#39;men&#39;=>&#39;&#39;,&#39;meng&#39;=>&#39;&#39;,&#39;mi&#39;=>&#39;&#39;,&#39;mian&#39;=>&#39;&#39;,&#39;miao&#39;=>&#39;&#39;,&#39;mie&#39;=>&#39;&#39;,&#39;min&#39;=>&#39;&#39;,&#39;ming&#39;=>&#39;&#39;,&#39;miu&#39;=>&#39;&#39;,&#39;mo&#39;=>&#39;&#39;,&#39;mou&#39;=>&#39;&#39;,&#39;mu&#39;=>&#39;&#39;,&#39;na&#39;=>&#39;&#39;,&#39;nai&#39;=>&#39;&#39;,&#39;nan&#39;=>&#39;&#39;,&#39;nang&#39;=>&#39;&#39;,&#39;nao&#39;=>&#39;&#39;,&#39;ne&#39;=>&#39;&#39;,&#39;nei&#39;=>&#39;&#39;,&#39;nen&#39;=>&#39;&#39;,&#39;neng&#39;=>&#39;&#39;,&#39;ni&#39;=>&#39;&#39;,&#39;nian&#39;=>&#39;&#39;,&#39;niang&#39;=>&#39;&#39;,&#39;niao&#39;=>&#39;&#39;,&#39;nie&#39;=>&#39;&#39;,&#39;nin&#39;=>&#39;&#39;,&#39;ning&#39;=>&#39;&#39;,&#39;niu&#39;=>&#39;&#39;,&#39;nong&#39;=>&#39;&#39;,&#39;nu&#39;=>&#39;&#39;,&#39;nv&#39;=>&#39;&#39;,&#39;nuan&#39;=>&#39;&#39;,&#39;nue&#39;=>&#39;&#39;,&#39;nuo&#39;=>&#39;&#39;,&#39;ou&#39;=>&#39;&#39;,&#39;pa&#39;=>&#39;&#39;,&#39;pai&#39;=>&#39;&#39;,&#39;pan&#39;=>&#39;&#39;,&#39;pang&#39;=>&#39;&#39;,&#39;pao&#39;=>&#39;&#39;,&#39;pei&#39;=>&#39;&#39;,&#39;pen&#39;=>&#39;&#39;,&#39;peng&#39;=>&#39;&#39;,&#39;pi&#39;=>&#39;&#39;,&#39;pian&#39;=>&#39;&#39;,&#39;piao&#39;=>&#39;&#39;,&#39;pie&#39;=>&#39;&#39;,&#39;pin&#39;=>&#39;&#39;,&#39;ping&#39;=>&#39;&#39;,&#39;po&#39;=>&#39;&#39;,&#39;pou&#39;=>&#39;&#39;,&#39;pu&#39;=>&#39;&#39;,&#39;qi&#39;=>&#39;&#39;,&#39;qia&#39;=>&#39;&#39;,&#39;qian&#39;=>&#39;&#39;,&#39;qiang&#39;=>&#39;&#39;,&#39;qiao&#39;=>&#39;&#39;,&#39;qie&#39;=>&#39;&#39;,&#39;qin&#39;=>&#39;&#39;,&#39;qing&#39;=>&#39;&#39;,&#39;qiong&#39;=>&#39;&#39;,&#39;qiu&#39;=>&#39;&#39;,&#39;qu&#39;=>&#39;&#39;,&#39;quan&#39;=>&#39;&#39;,&#39;que&#39;=>&#39;&#39;,&#39;qun&#39;=>&#39;&#39;,&#39;ran&#39;=>&#39;&#39;,&#39;rang&#39;=>&#39;&#39;,&#39;rao&#39;=>&#39;&#39;,&#39;re&#39;=>&#39;&#39;,&#39;ren&#39;=>&#39;&#39;,&#39;reng&#39;=>&#39;&#39;,&#39;ri&#39;=>&#39;&#39;,&#39;rong&#39;=>&#39;&#39;,&#39;rou&#39;=>&#39;&#39;,&#39;ru&#39;=>&#39;&#39;,&#39;ruan&#39;=>&#39;&#39;,&#39;rui&#39;=>&#39;&#39;,&#39;run&#39;=>&#39;&#39;,&#39;ruo&#39;=>&#39;&#39;,&#39;sa&#39;=>&#39;&#39;,&#39;sai&#39;=>&#39;&#39;,&#39;san&#39;=>&#39;&#39;,&#39;sang&#39;=>&#39;&#39;,&#39;sao&#39;=>&#39;&#39;,&#39;se&#39;=>&#39;&#39;,&#39;sen&#39;=>&#39;&#39;,&#39;seng&#39;=>&#39;&#39;,&#39;sha&#39;=>&#39;&#39;,&#39;shai&#39;=>&#39;&#39;,&#39;shan&#39;=>&#39;&#39;,&#39;shang&#39;=>&#39;&#39;,&#39;shao&#39;=>&#39;&#39;,&#39;she&#39;=>&#39;&#39;,&#39;shen&#39;=>&#39;&#39;,&#39;sheng&#39;=>&#39;&#39;,&#39;shi&#39;=>&#39;&#39;,&#39;shou&#39;=>&#39;&#39;,&#39;shu&#39;=>&#39;&#39;,&#39;shua&#39;=>&#39;&#39;,&#39;shuai&#39;=>&#39;&#39;,&#39;shuan&#39;=>&#39;&#39;,&#39;shuang&#39;=>&#39;&#39;,&#39;shui&#39;=>&#39;&#39;,&#39;shun&#39;=>&#39;&#39;,&#39;shuo&#39;=>&#39;&#39;,&#39;si&#39;=>&#39;&#39;,&#39;song&#39;=>&#39;&#39;,&#39;sou&#39;=>&#39;&#39;,&#39;su&#39;=>&#39;&#39;,&#39;suan&#39;=>&#39;&#39;,&#39;sui&#39;=>&#39;&#39;,&#39;sun&#39;=>&#39;&#39;,&#39;suo&#39;=>&#39;&#39;,&#39;ta&#39;=>&#39;&#39;,&#39;tai&#39;=>&#39;&#39;,&#39;tan&#39;=>&#39;&#39;,&#39;tang&#39;=>&#39;&#39;,&#39;tao&#39;=>&#39;&#39;,&#39;te&#39;=>&#39;&#39;,&#39;teng&#39;=>&#39;&#39;,&#39;ti&#39;=>&#39;&#39;,&#39;tian&#39;=>&#39;&#39;,&#39;tiao&#39;=>&#39;&#39;,&#39;tie&#39;=>&#39;&#39;,&#39;ting&#39;=>&#39;&#39;,&#39;tong&#39;=>&#39;&#39;,&#39;tou&#39;=>&#39;&#39;,&#39;tu&#39;=>&#39;&#39;,&#39;tuan&#39;=>&#39;&#39;,&#39;tui&#39;=>&#39;&#39;,&#39;tun&#39;=>&#39;&#39;,&#39;tuo&#39;=>&#39;&#39;,&#39;wa&#39;=>&#39;&#39;,&#39;wai&#39;=>&#39;&#39;,&#39;wan&#39;=>&#39;&#39;,&#39;wang&#39;=>&#39;&#39;,&#39;wei&#39;=>&#39;&#39;,&#39;wen&#39;=>&#39;&#39;,&#39;weng&#39;=>&#39;&#39;,&#39;wo&#39;=>&#39;&#39;,&#39;wu&#39;=>&#39;&#39;,&#39;xi&#39;=>&#39;&#39;,&#39;xia&#39;=>&#39;&#39;,&#39;xian&#39;=>&#39;&#39;,&#39;xiang&#39;=>&#39;&#39;,&#39;xiao&#39;=>&#39;&#39;,&#39;xie&#39;=>&#39;&#39;,&#39;xin&#39;=>&#39;&#39;,&#39;xing&#39;=>&#39;&#39;,&#39;xiong&#39;=>&#39;&#39;,&#39;xiu&#39;=>&#39;&#39;,&#39;xu&#39;=>&#39;&#39;,&#39;xuan&#39;=>&#39;&#39;,&#39;xue&#39;=>&#39;&#39;,&#39;xun&#39;=>&#39;&#39;,&#39;ya&#39;=>&#39;&#39;,&#39;yan&#39;=>&#39;&#39;,&#39;yong&#39;=>&#39;&#39;,&#39;yao&#39;=>&#39;&#39;,&#39;ye&#39;=>&#39;&#39;,&#39;yi&#39;=>&#39;&#39;,&#39;yin&#39;=>&#39;&#39;,&#39;ying&#39;=>&#39;&#39;,&#39;yo&#39;=>&#39;&#39;,&#39;yong&#39;=>&#39;&#39;,&#39;you&#39;=>&#39;&#39;,&#39;yu&#39;=>&#39;&#39;,&#39;yuan&#39;=>&#39;&#39;,&#39;yue&#39;=>&#39;&#39;,&#39;yun&#39;=>&#39;&#39;,&#39;za&#39;=>&#39;&#39;,&#39;zai&#39;=>&#39;&#39;,&#39;zan&#39;=>&#39;&#39;,&#39;zang&#39;=>&#39;&#39;,&#39;zao&#39;=>&#39;&#39;,&#39;ze&#39;=>&#39;&#39;,&#39;zei&#39;=>&#39;&#39;,&#39;zen&#39;=>&#39;&#39;,&#39;zeng&#39;=>&#39;&#39;,&#39;zha&#39;=>&#39;&#39;,&#39;zhai&#39;=>&#39;&#39;,&#39;zhan&#39;=>&#39;&#39;,&#39;zhong&#39;=>&#39;&#39;,&#39;zhao&#39;=>&#39;&#39;,&#39;zhe&#39;=>&#39;&#39;,&#39;zhen&#39;=>&#39;&#39;,&#39;zheng&#39;=>&#39;&#39;,&#39;zhi&#39;=>&#39;&#39;,&#39;zhong&#39;=>&#39;&#39;,&#39;zhou&#39;=>&#39;&#39;,&#39;zhu&#39;=>&#39;&#39;,&#39;zhua&#39;=>&#39;&#39;,&#39;zhuai&#39;=>&#39;&#39;,&#39;zhuan&#39;=>&#39;&#39;,&#39;zhuang&#39;=>&#39;&#39;,&#39;zhui&#39;=>&#39;&#39;,&#39;zhun&#39;=>&#39;&#39;,&#39;zhuo&#39;=>&#39;&#39;,&#39;zi&#39;=>&#39;&#39;,&#39;zong&#39;=>&#39;&#39;,&#39;zou&#39;=>&#39;&#39;,&#39;zu&#39;=>&#39;&#39;,&#39;zuan&#39;=>&#39;&#39;,&#39;zui&#39;=>&#39;&#39;,&#39;zun&#39;=>&#39;&#39;,&#39;zuo&#39;=>&#39;&#39;);
function changed_pinyin($str) { //判断是否是 单拼  双拼 三拼 四拼
 
    global $pin_arr;
 
    if(isset($pin_arr[$str])){
        return 1;
    }
    
    $length = strlen($str);
    for ($i = 1; $i < $length; $i++) {
 
        if ($i < 7) {
            if (changed_pinyin(substr($str, 0, $i)) && $n = changed_pinyin(substr($str, $i))) {
                return $n + 1;
            }
        } else {
            return 0;
        }
    }
    return 0;
}
 
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
 
 
//随机生成字符串用于测试
$random_array = array();
for($i = 0; $i < 5000; $i++){
    $str = array_merge(range(0,9),range(&#39;a&#39;,&#39;z&#39;));
    shuffle($str);
    $random_array[] = implode(&#39;&#39;,array_slice($str,0,  array_rand(range(2, 15))));
}
 
$time_start = microtime_float();
 
foreach($random_array as $row){
    changed_pinyin($row);  //大于30s
    //pinyin($row);        //小于0.5s
}
 
$time_end = microtime_float();
$time = $time_end - $time_start;
 
echo "耗时: $time seconds\n";

很显然上面这个函数主要一直在执行in_array()函数,所以第一步就是优化in_array()函数:
这里我了解到PHP Array的KEY是进行HASH组织的,查询很快。而VALUE是由KEY组织存放,本身没有索引,每次查找都是遍历。
于是,我将in_array()那段改了下:
 

$pin_arr    =  array_flip($pin_arr);
if(array_key_exists($str,$pin_arr)){
	return 1; 
}


效率提高不明显,考虑到$pin_arr数组太大,于是我将其作为全局变量,提到外面,于是时间缩短了几十倍,对于判断数组元素是否存在,通过测试发现使用array_key_exists随着循环的次数增加,程序运行的时间增加量是成几何级数增加的,当一个数组元素个数超过1000时运行速度就非常慢了,isset()的效率要远高于array_key_exists()。于是效率又提高了几倍。
最后当然是把执行函数strlen($str)提到循环外,这不用多说了吧!
 


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn