13

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?

Rounds
  • 1,879
  • 4
  • 19
  • 30
  • Go to http://eval.in and test it? – Niels Keurentjes Aug 25 '14 at 08:19
  • 1
    Why don't you benchmark it? It may theoretically also vary dramatically between different PHP versions, so it's hardly a constructive question. Few performance questions are. – deceze Aug 25 '14 at 08:20
  • 1
    Given that you're not using the keys of the array, why not compare these two approaches to a simple `for` loop, too? `for` loops tend to outperform `foreach`, and seeing as loops are constructs, and not function calls, I'd expect them to outperform `array_map` – Elias Van Ootegem Aug 25 '14 at 08:22
  • You can test one of the solution given by [PHP code's performance test](http://stackoverflow.com/q/2919543/3361444). Like says deceze, performance can change following your PHP version. – Debflav Aug 25 '14 at 08:24

3 Answers3

8

For the record (php 7.4 + 64bits + windows)

https://github.com/EFTEC/php-benchmarks/blob/master/benchmark_arraymap_foreach.php

  • foreach = 0.10213899612427
  • array_map = 0.18259811401367
  • array_map (static) = 0.18230390548706
  • array_map (calling a function) = 0.17731499671936

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);
magallanes
  • 6,583
  • 4
  • 54
  • 55
6

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.

ChristoKiwi
  • 308
  • 5
  • 10
1

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!