php中使用in_array() foreach array_search() 查找数组是否包含时的性能对比,phpforeach二维数组
php中使用in_array() foreach array_search() 查找数组是否包含时的性能对比,phpforeach二维数组
判断某字符是否包含与某于数组中,方法有很多,刚学习php的新手们估计偏向于使用循环来解决,对于一般的小网站来说,这种解决方案是不会出现什么大问题的。但就性能来说,这种方法不是最好的方法,下面笔者就 foreach,in_array() array_search 这三种方法来比较这三种方法在性能表现上的差异。
<?php $runtime= new runtime; $runtime->start(); $a = 'k'; $b = array('a','b','c','d','e','f','g','h','i','j','k'); /* for ($i=0; $i < 100000; $i++) { var_dump(in_array($a, $b)); } */ /* for ($i=0; $i < 100000; $i++) { foreach ($b as $key => $value) { if ($a == $value) { //echo TRUE; continue; } } } */ /* for ($i=0; $i < 100000; $i++) { array_search($a, $b); } */ $runtime->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

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



1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

With the continuous development of internationalization, more and more websites and applications need to support multi-language switching functions. As a popular front-end framework, Vue provides a plug-in called i18n that can help us achieve multi-language switching. This article will introduce common techniques for using i18n to implement multi-language switching in Vue. Step 1: Install the i18n plug-in First, we need to install the i18n plug-in using npm or yarn. Enter the following command at the command line: npminst

The out interface refers to the output interface, and the in interface refers to the input interface. The out interface generally represents the audio source line output interface, which is used to connect loads, such as speakers, headphones, etc.; while the in interface generally represents the audio source line input interface, which is used to connect CD players, mobile phones, MP3 players, computers, etc.

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values swapped. $original_array=[

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb
