Search
 
SCRIPT & CODE EXAMPLE
 

PHP

array filter vs array search php

<?php
/*** Results on my machine ***
php 7.2
array_filter: 2.5147440433502
foreach: 0.13733291625977
for i: 0.24090600013733

php 7.4
array_filter: 0.057109117507935
foreach: 0.021071910858154
for i: 0.027867078781128
**/

ini_set('memory_limit', '500M');
$data = range(0, 1000000);

// ARRAY FILTER
$start = microtime(true);
$newData = array_filter($data, function ($item) {
    return $item % 2;
});
$end = microtime(true);

echo "array_filter: ";
echo $end - $start . PHP_EOL;

// FOREACH
$start = microtime(true);
$newData = array();
foreach ($data as $item) {
    if ($item % 2) {
        $newData[] = $item;
    }
}
$end = microtime(true);

echo "foreach: ";
echo $end - $start . PHP_EOL;

// FOR
$start = microtime(true);
$newData = array();
$numItems = count($data);
for ($i = 0; $i < $numItems; $i++) {
    if ($data[$i] % 2) {
        $newData[] = $data[$i];
    }
}
$end = microtime(true);

echo "for i: ";
echo $end - $start . PHP_EOL;
Comment

PREVIOUS NEXT
Code Example
Php :: wp plugin handles deregister 
Php :: Laravel route returning error 404 when attempt is made to pass value to controller function 
Php :: laravel eloquent where date today 
Php :: instagram api error 
Php :: How to increase the WordPress Multisite Network limit for Maximum Filesize Upload? 
Php :: laravel-filemanager 404 error 
Php :: doiffernce between text and string in laravel migration 
Php :: run drush command from php 
Php :: how to search locations in php using ajax google script 
Php :: gerar aquivo csv php 
Php :: event handler with worker laravel 
Php :: wordpress wpdb delete 
Php :: login php 
Php :: modal form with php 
Php :: timestamp in model laravel 
Php :: wordpress change permalink on upload 
Php :: php catch mysqli_connect(): (HY000/1045): Access denied 
Php :: image::make en php 
Php :: recorrer un array php 
Php :: php time passed since date 
Java :: java get screen size 
Java :: round jframe corners in java 
Java :: make javafx open full screen 
Java :: java android show toast 
Java :: will my java minecraft be discontinued 
Java :: random object of array java 
Java :: bucket sort java 
Java :: java format 2 decimal places 
Java :: capcitor FERR_CLEARTEXT_NOT_PERMITTED 
Java :: java remove last character from string 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =