In terms of performance, which is the better option?
While in object:
Case #1
public function test( $array ) {
return array_map( array( $this, 'do_something_to_element' ), $array );
}
Case #2
public function test( $array ) {
$return = array();
foreach ( $array as $value ) {
$return[] = do_something_to_element( $value );
}
return $return;
}
There are other uses of course and many many examples can be populated. I've seen comments that while in an object, array_map is slower than foreach loops.
In general is the array_map/array_walk functions faster to execute than the foreach loops in similar needs?
The 'foreach' is slow in comparison to the 'for' loop. The foreach copies the array over which the iteration needs to be performed. For improved performance, the concept of references needs to be used.
The array_map() is an inbuilt function in PHP and it helps to modify all elements one or more arrays according to some user-defined condition in an easy manner. It basically, sends each of the elements of an array to a user-defined function and returns an array with new values as modified by that function.
For the record (php 7.4 + 64bits + windows)
https://github.com/EFTEC/php-benchmarks/blob/master/benchmark_arraymap_foreach.php
Foreach is still faster but also if we use a static function or not, it doesn't make any difference:
$result = array_map(function ($number) {
return $number * 10;
}, $numbers);
$result = array_map(static function ($number) {
return $number * 10;
}, $numbers);
I believe this answers your question and is current as of 2015-01-22
Performance of foreach, array_map with lambda and array_map with static function
array_map although more elegant is sadly slower in PHP. Particularly if using it with a closure.
I tested this on a Symfony project just now, had to Google because it seems so significant.
Script went from 160ms using foreach()
to 260ms using array_map()
. Considering the size of the application thats quite a large increase from a single method call.
i am here now 'cause i was googleize the words : "php 8 foreach most fastest" and do not threat his question as duplicate please 'cause i wrote in all php versions the fastest php framework in the world(classic structural,oop , oop at sum based static public functions) and in particular in the php 7 the array_filter i every time use was fastest in many cases than foreach ,now i use in xampp windows 10 x64 the same methods to benchmark and i got fastest results on foreach. so the answer on your question is YES INDEED IS FOREACH FASTEST so use base that in php 8 and the other code in php 7 or less !!!
here is the versions of thecode i test(i was modify a simple script to popup all new bootstrap icons 1.4.1 on the screen and):
the first code i test and if you uncoment parts of it and the correct parts for combining you got some little differences but idea remain the same
$icons.=<<<icon
{$pa} {$pa2}
icon;
/*
$icons.=<<<icon
{$pa}
icon;
*/
//});
//},ARRAY_FILTER_USE_KEY);
},ARRAY_FILTER_USE_BOTH);
print_r($icons);
//0.00001200
the equivalent code i test
$icons='';
foreach([0=>1,1=>99] as $ch => $vl){
$icons.=<<<icon
{$ch} {$vl}
icon;
}
print_r($icons);
//0.00000694
i bench it with a "plugin" improvisation =a cli php code called with a .exe purebasic from inside a editor doesn't have plugin possibilities( editplus ) but have user text filters so you see at the end of each script the result of the php script:
if ($argv[1]=='bench'){
$cmd=cmd(1);
ob_start();
$start= microtime(true);
for($i=0;$i<10000;$i++) eval($cmd);
$stop= microtime(true);
ob_clean ();
echo PHP_EOL.'//'. number_format(($stop-$start)/10000, 8, '.', '');
}
I am a public static function "consumer" too ! cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With