php中使用in_array() foreach array_search() 查找数组是否包含时的性能对比
这篇文章主要介绍了php中使用in_array() foreach array_search() 查找数组是否包含时的性能对比,需要的朋友可以参考下
判断某字符是否包含与某于数组中,方法有很多,刚学习php的新手们估计偏向于使用循环来解决,对于一般的小网站来说,这种解决方案是不会出现什么大问题的。但就性能来说,这种方法不是最好的方法,下面笔者就 foreach,in_array() array_search 这三种方法来比较这三种方法在性能表现上的差异。
start(); $a = 'k'; $b = array('a','b','c','d','e','f','g','h','i','j','k'); /* for ($i=0; $i $value) { if ($a == $value) { //echo TRUE; continue; } } } */ /* for ($i=0; $i stop(); echo $_b; echo "执行时间: ".$runtime->spent()." 毫秒"; class runtime{ var $StartTime = 0; var $StopTime = 0; function get_microtime(){ list($usec, $sec) = explode(' ', microtime()); return ((float)$usec + (float)$sec); } function start(){ $this->StartTime = $this->get_microtime(); } function stop(){ $this->StopTime = $this->get_microtime(); } function spent(){ return round(($this->StopTime - $this->StartTime) * 1000, 1); } } ?>
以上程序执行时间如下图所示:
in_array()
foreach
array_search()
由上可以大致看出这三种方法在性能上的表现了吧,array_search 和 in_array 表现差不多,foreach 表现最差。
,
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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

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.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools