search
HomeBackend DevelopmentPHP Tutorial优化PHP in_array()函数,效率提高50倍

/原始程序
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)提到循环外,这不用多说了吧!
 


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)