Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping in function or multiple calling the function, what is faster?

Tags:

loops

php

basicly I've got an array and want to call the same function for each element. Which way is it faster?

foreach($elemeents as $element){
    callFunction($element);
}

OR

function callFunction($leements){
    foreach($elements as $element){
        //do something
    }
}

thanx in advance, im just a beginner

like image 523
Adriana Avatar asked Oct 28 '25 04:10

Adriana


1 Answers

Probably slightly faster with the loop inside the function, as there is a (slight) cost to each function call. However, it won't make much difference.

This is really premature optimization, and the root of all evil.

You should write it so it is clear, then if it's too slow, figure out where it's slow and optimize that.

like image 162
Blorgbeard Avatar answered Oct 29 '25 19:10

Blorgbeard