Optimized the PHPin_array () function to increase the efficiency by 50 times.

Source: Internet
Author: User
The first article of the year, working the day after tomorrow, is being adjusted. My roommate asked me a piece of program efficiency question a year ago. I just came to study it! This function is used to determine whether a domain name string is a single domain name, a double domain name, or a three-domain name... Question about multiple domain names: // original program function

The first article of the year, working the day after tomorrow, is being adjusted. My roommate asked me a piece of program efficiency question a year ago. I just came to study it! This function is aboutDetermine whether the domain name string is a single domain name, a double domain name, or a three-domain name... Questions about multiple domain names:

// Original program function pinyin ($ str) {// judge whether it is a single fight, a single fight, and a single fight. $ pin_arr = array ("a", "ai", "", "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", "hao ", "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 ;}

The above $ pin_arr is a large array with approximately 400 elements. from the above, we can see that the single-domain name, double-domain name, and three-domain name concepts: if a domain name [here $ str refers to the intermediate part of the domain name, for example, www.phpddt.com is phpddt. all elements in $ pin_arr are composed of n domain names.
Look at the optimized program code:
/*** Function for determining n domain names after Optimization ** @ copyright http://www.phpddt.com */$ Pin_arr = array ('a' => '', 'Ai' =>'', 'any' => '', 'ang '=> '', 'ao' => '', 'ba' =>'', 'ba' => '', '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' => '', 'CA' =>'', 'ch' => '', 'Chen '=> '', 'cheng' =>'', 'Chi' => '', 'Chong '=>'', 'chou' => '', 'chu' => '', 'chuai' =>'', 'chuanc' => '', 'chuang '=>'', 'chui' => '', 'chun' => '', 'chuo' =>'', 'ci' => '', 'cong' =>'', 'cou' => '', 'cu '=> '', 'chuanc' =>'', 'Cui' => '', 'cun' =>'', 'cuo' => '', 'da' => '', 'Dai '=>'', 'Dan' => '', 'Dang' =>'', 'Dao '=> '', 'dep' => '', 'deng' =>'', 'di' => '', 'Dian' =>'', 'diao' => '', 'die' => '', 'Ding' =>'', 'dui' => '', 'dong' =>'', 'dou' => '', 'du' => '', 'duany' =>'', 'dui' => '', 'dun' =>'', 'Duo' => '', 'E' => '', 'en' =>'', 'ER' => '', 'FA '=>'', 'fan' => '', 'fang '=> '', 'fei' =>'', 'fen' => '', 'feng' =>'', 'fo' => '', 'fou' => '', 'Fu' =>'', 'ga '=> '', 'gai' =>'', 'Gan' => '', 'giang '=> '', 'gao' =>'', 'ge' => '', 'gei' =>'', 'Gen' => '', 'geng' => '', 'Gong '=>'', 'gou' => '', 'GU' =>'', 'gua' => '', 'guai' => '', 'guany' =>'', 'guang '=> '', 'gui' =>'', 'gun' => '', 'guo' => '', 'ha' =>'', 'Hai' => '', 'Han' =>'', 'hang' => '', 'hao' => '', 'hes' =>'', 'hei' => '', 'hangzhou' =>'', 'hangzhou' => '', 'Hong '=> '', 'hou' =>'', 'hu' => '', 'hua' =>'', 'huai' => '', 'huany' => '', 'huangt' =>'', 'hui' => '', 'Hun' =>'', 'huo' => '', 'Ji '=> '', 'Jia' =>'', 'jian '=> '', 'Jiang' =>'', 'jiao' => '', 'jie' => '', 'Jin' =>'', 'Jing' => '', 'jiong '=>'', 'jiu' => '', 'ju' => '', 'juany' =>'', 'jue '=> '', 'Jun' =>'', 'CA' => '', 'Kai' => '', 'kan '=>'', 'kang' => '', 'kakao' =>'', 'KE' => '', 'kei' => '', 'Ken' =>'', 'keng' => '', 'Kong '=>'', 'kou' => '', 'ku '=> '', 'kua' =>'', 'kuai' => '', 'kuany' =>'', 'kuang' => '', 'kuai' => '', 'Ku' =>'', 'kuo' => '', 'La '=>'', 'lay' => '', 'lan' => '', 'Lang '=>'', 'Lao' => '', 'le' =>'', 'lei' => '', 'leng' => '', 'Lil' =>'', 'Lil' => '', 'liany' =>'', 'Shanghai' => '', 'liao' => '', 'Lil' =>'', 'lin' => '', 'Ling' =>'', 'liu' => '', 'Long' => '', 'Lou '=>'', 'Lu' => '', 'LV' =>'', 'luan' => '', 'lue' => '', 'lun '=>'', 'luo' => '', 'M' =>'', 'Mai' => '', 'Man '=> '', 'MANG' =>'', 'Mao' => '', 'Me '=>'', 'Mei' => '', 'Men '=> '', 'meng' =>'', 'MI' => '', 'Mian' =>'', 'Miao' => '', 'Min' => '', 'min' =>'', 'Ming' => '', 'miu' =>'', 'Mo' => '', 'MOU' => '', 'Mu '=>'', 'na' => '', 'Nai' =>'', 'Nan' => '', 'nang '=> '', 'nao' =>'', 'ne' => '', 'nei' =>'', 'nen' => '', 'neng' => '', 'ni' =>'', 'niany' => '', 'niang '=>'', 'niao' => '', ''ni'' => '', 'nin' =>'', 'ning' => '', 'Niu' =>'', 'Nong '=> '', 'nu '=> '', 'nv' =>'', 'nuany' => '', 'Nue '=>'', 'nuo' => '', 'ou '=> '', 'pa' =>'', 'Pai' => '', 'pan' =>'', 'pang '=> '', 'pao' => '', 'pei '=>'', 'pen' => '', 'Peng' =>'', 'pi '=> '', 'piany' => '', 'piao' =>'', 'pie' => '', 'pin' =>'', 'ping' => '', 'Po' => '', 'pou' =>'', 'pu' => '', 'qi '=>'', 'qia' => '', 'Qian '=> '', 'Qiang' =>'', 'qiao' => '', 'qie' =>'', 'Qin' => '', 'Qing' => '', 'qiong' =>'', 'Qiu '=> '', 'qu' =>'', 'quany' => '', 'que '=> '', 'qun' =>'', 'run' => '', 'rang' =>'', 'rao' => '', 're' => '', 'ren' =>'', 'reng' => '', 'Ri' =>'', 'Rong '=> '', 'rou' => '', 'Ru '=>'', 'rule' => '', 'Rui' =>'', 'run' => '', 'ruo' => '', 'sa '=>'', 'Sai' => '', 'san' =>'', 'Shanghai' => '', 'sao' => '', 'se' =>'', 'sen' => '', 'seng' =>'', 'Sha' => '', 'shai' => '', 'shany' =>'', 'shang' => '', 'shao' =>'', 'she' => '', 'shen' => '', 'sheng' =>'', 'shi' => '', 'Shou' =>'', 'shu' => '', 'shua '=> '', 'shuai' =>'', 'shuany' => '', 'shuang' =>'', 'shui '=> '', 'Shanghai' => '', 'Shanghai' =>'', 'Si' => '', 'song' =>'', 'sou' => '', 'su' => '', 'suany' =>'', 'sui' => '', 'sun' =>'', 'suo' => '', 'ta' => '', 'Tai' =>'', 'tan' => '', 'tang' =>'', 'ta' => '', 'ta' => '', 'Te' => '', 'teng' =>'', 'ti' => '', 'tiany' =>'', 'tiao' => '', 'tie' => '', 'ting' =>'', 'tong' => '', 'tou' =>'', 'tu' => '', 'tuany' => '', 'Tui '=>'', 'tun' => '', 'tuo' =>'', 'wa '=> '', 'wai' => '', 'wan '=>'', 'Wang' => '', 'Wei' =>'', 'wen' => '', 'engg' => '', 'wo' =>'', 'wu' => '', 'Si' =>'', 'Xia '=> '', 'xian '=> '', 'Xiang' =>'', 'Xiao' => '', 'xie' =>'', 'sin' => '', 'X' => '', 'xiong' =>'', 'Xiu' => '', 'Xu '=>'', 'xuany' => '', 'xue '=> '', 'xun' =>'', 'Ya '=> '', 'Yan' =>'', 'Yong '=> '', 'yunao' => '', 'Ye '=>'', 'yi' => '', 'in' =>'', 'ying' => '', 'yo' => '', 'Yong '=>'', 'you' => '', 'Yu' =>'', 'yuany' => '', 'yue' => '', 'yun '=>'', 'Za' => '', 'zai' =>'', 'zany' => '', 'zang '=> '', 'zao' =>'', 'ze' => '', 'Zei' =>'', 'zn' => '', 'zeng' => '', 'zha' =>'', 'Zhai' => '', 'zhan' =>'', 'zhong' => '', 'zhao' => '', 'zhes' =>'', 'Zhen' => '', 'zheng' =>'', 'zhi' => '', 'zhong' => '', 'zhou' =>'', 'zhu' => '', 'zhua' =>'', 'zhuai' => '', 'zhuany' => '', 'zhuang' =>'', 'zhui' => '', 'zhun' =>'', 'zhuo' => '', 'zi' => '', 'zong' =>'', 'zou' => '', 'Zu' =>'', 'zuan' => '', 'zui '=> '', 'Zun' =>'', 'zuo' => ''); function changed_pinyin ($ str) {// determine whether it is a single fight, double fight, three fight, four fight 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);} // randomly generated string for testing $ random_array = array (); for ($ I = 0; $ I <5000; $ I ++) {$ str = array_merge (range (), range ('A', 'z ')); shuffle ($ str); $ random_array [] = implode ('', array_slice ($ str, 0, array_rand (range (2, 15 ))));} $ time_start = microtime_float (); foreach ($ random_array as $ row) {changed_pinyin ($ row); // greater than 30 s // pinyin ($ row ); // less than 0.5 s} $ time_end = microtime_float (); $ time = $ time_end-$ time_start; echo "time consumed: $ time seconds \ n ";

Obviously, the above function is mainly executing the in_array () function, so The first step is to optimize the in_array () function:
Here I learned that the KEY of the PHP Array is organized by HASH, and the query is fast. The VALUE is stored by the KEY organization, and there is no index in itself. every search is traversed.
So I changed the in_array () section to the following:
$pin_arr    =  array_flip($pin_arr);if(array_key_exists($str,$pin_arr)){return 1; }

Efficiency improvement is not obvious, Considering that the $ pin_arr array is too large, I use it as a global variable.When it comes to the outside, the time is reduced by dozens of times. to determine whether an array element exists, we can test that the use of array_key_exists increases with the number of cycles, the increase in program running time is a geometric increase. when the number of elements in an array exceeds 1000, the operation speed is very slow, The efficiency of isset () is much higher than that of array_key_exists (). As a result, the efficiency has increased several times.
Finally Execution function strlen ($ str) mentions loopYou don't have to say that!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.