Forum Moderators: coopster

Message Too Old, No Replies

Calling functions in a loop? Too much memory used?

Performance issues...

         

Matthew1980

7:14 pm on Oct 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there people of the forum,

(PSUEDO CODE!)

$day = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
$days = count($day);


foreach($day as $key=>$value){
if(preg_match("/".$value."/", end($errorLogContents))){
echo "Match<br>";
if($value != date("l")){
echo "Previous day logged was ".$value." and todays day is ".date("l")."<br>";
}
}
else{
echo "No Match<br>";
}
}


My thought is, I use the count() outside the loop so as not to incur too much parser/cpu load, but then I go on to use end() inside the loop.. Kinda contradictory, and as my file could either hold 5 lines or 500 lines, I would like to know whether or not I should worry about using end() within the loop, of whether assigning it to a variable will take up more memory than repeated calls when the loop is invoked..

I personally think that as the loop is only called 7 times, and once the loop has finished, that 'temp' memory has been released, that the assignment to a var could incur a larger memory usage than the loop - or am I talking/thinking stupidly here.

As it stands it works, but I am trying to write this system for work with efficiency in mind..

Cheers,
MRb

Anyango

7:42 pm on Oct 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if all the 7 times this end function returns the same result then i would call that function only once , outside the loop. Its true that assigning to a variable will take up some space but i think its far better than using that into a loop 7 times over, Because remember whenever a function is called there are many things cpu has to do, it has to update many variables and stacks before calling a function, and then pop those values out of its call stack when the function returns ( multiplied by 7 ). Its alot more overhead this way i believe.

Also if assigning a value to a variable takes memory space, so be it. If it takes that to get the job done, no harm then.

Edit:
Memory will be used in both these cases, in case of 7 function calls more memory will be used. The only difference will be that if you go 1 call way then you are the one using memory to get the job done, if you go 7 calls way then cpu is the one using that memory on your behalf to get the job done

Matthew1980

7:50 pm on Oct 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Anyango,

It should return the last entry that was generated from the last 24 hours, so initially the contents of the file will be/should be quite small, but over time I expect it to get quite sizeable, which if it does, I shall write an 'archive' type catch/clause into it anyway..

Thanks for confirming what I suspected, I have since altered the code anyway, I just had to ask because I don't usually worry about this sort of stuff, but this intranet system I am doing for work is potentially going to be used by all departments, so quite a load on the server.

Cheers,
MRb