function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
guest1231231guest1231231 

Which Loop Is More Efficient?

Which for loop here is more efficient, is there even a difference when it comes to avoiding governor limits?

 

 

Task[] tasks = new Task[]{};
for(Integer i=0;i<tasks.size();i++) {
//some code
}

 

 

OR

 

 

Task[] tasks = new Task[]{};
for(Task activity: tasks) {
//some code
}

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
rungerrunger

The latter loop is marginally more efficient in terms of the "statements executed" limit, but really they're basically the same.

 

Rich

All Answers

rungerrunger

The latter loop is marginally more efficient in terms of the "statements executed" limit, but really they're basically the same.

 

Rich

This was selected as the best answer
guest1231231guest1231231

I'd figure they were about the same but wasn't sure.  That makes sense that the latter would be more efficient as far as statements go since their are less arguements.

 

-Thanks

rungerrunger

What you're saving there is the statements for declaring and incrementing the counter variable.

Nathan CrosbyNathan Crosby

I'd add that the real impact on governor limits is going to be the code that exists in that loop. If you're executing any SOQL for example, you can be certain you'll hit your query limits.

gv007gv007

This for loop changed in all major programmming languages,people suggest that interm of excution second one is best.