优化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('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'=>''); 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('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); //大于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)提到循环外,这不用多说了吧!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











At present, PHP has become one of the most popular programming languages in Internet development, and the performance optimization of PHP programs has also become one of the most pressing issues. When handling large-scale concurrent requests, a delay of one second can have a huge impact on the user experience. Today, APCu (AlternativePHPCache) caching technology has become one of the important methods to optimize PHP application performance. This article will introduce how to use APCu caching technology to optimize the performance of PHP applications. 1. APC

With the development of the Internet, PHP applications have become more and more common in the field of Internet applications. However, high concurrent access by PHP applications can lead to high CPU usage on the server, thus affecting the performance of the application. In order to optimize the performance of PHP applications, Memcached caching technology has become a good choice. This article will introduce how to use Memcached caching technology to optimize the CPU usage of PHP applications. Introduction to Memcached caching technology Memcached is a

Overview of How to Optimize SuiteCRM's Client-Side Performance with PHP: SuiteCRM is a powerful open source customer relationship management (CRM) system, but performance issues can arise when handling large amounts of data and concurrent users. This article will introduce some methods to optimize SuiteCRM client performance through PHP programming techniques, and attach corresponding code examples. Using appropriate data queries and indexes Database queries are one of the core operations of a CRM system. In order to improve query performance, appropriate data query

How to optimize PHP's database connection and query performance? The database is an indispensable part of web development, and PHP, as a widely used server-side scripting language, its connection to the database and query performance are crucial to the performance of the entire system. This article will introduce some tips and suggestions for optimizing PHP database connection and query performance. Use persistent connections: In PHP, a database connection is established every time a database query is executed. Persistent connections can reuse the same database connection in multiple queries, thereby reducing

Methods to optimize function performance for different PHP versions include: using analysis tools to identify function bottlenecks; enabling opcode caching or using an external caching system; adding type annotations to improve performance; and selecting appropriate string concatenation and sorting algorithms according to the PHP version.

How to Optimize SuiteCRM’s User Interface with PHP SuiteCRM is a popular open source CRM (customer relationship management) software that provides powerful functionality and flexible customizability. However, when using SuiteCRM, you sometimes find that the user interface (UI) performs poorly or does not meet specific needs. At this time, we can optimize the user interface of SuiteCRM by using the PHP programming language to improve performance and meet specific needs. This article will introduce some optimizations for SuiteC

How to use PHP to optimize the project management functions of SuiteCRM SuiteCRM is a powerful open source customer relationship management (CRM) system that provides a wide range of functions and customizability. In terms of project management, SuiteCRM provides some basic functions, such as task assignment, progress tracking, and file sharing. However, sometimes we need to optimize project management capabilities based on specific business needs. In this article, we’ll cover how to leverage the PHP programming language to extend and optimize SuiteCRM’s

How to use PHP to optimize the effect of DreamWeaver's website building. In today's rise of the Internet, it is increasingly important to build an efficient and high-quality website. DedeCMS is a powerful website building system, but sometimes its default functions may not fully meet our needs. In this article, we will explore how to use PHP to optimize the effect of Dreamweaver website building, and provide some specific code examples. 1. Optimize website speed. Website speed is one of the important factors for user experience and SEO ranking. Website speed can be improved by optimizing PHP code.
