4

I'm trying to optimize some code and am going through some triggers. I'm wondering if there is a performance difference between

  1. running three methods in series - and in each one, I loop over Trigger.new twice to pull an Id and then update the record from the parent record - and
  2. combining the methods and looping only twice, but doing more script statements in each loop iteration

The total script statements executed will be identical. This question is specifically if the number of loops executing those script statements should have an effect on application speed.

  • 1
    I usually judge with regards to readability, maintainability and performance last, unless I know it's a long running operation. Can you provide something a little more concrete, though? Otherwise this is very broad. – Mike Chale Jan 7 '14 at 20:08
5

In general less loops is better, as this can lead to better chances to not thrash the L2 cache on the CPU, however there are so many levels between there and your apex code that the odds of you actually seeing any measurable difference is nil.

For triggers, performance is totally dominated by db access by a large margin, so that's the best place to concentrate on perf (make as few db calls as possible, access as little db data as possible, things that the governor limits already incentives you to do).

For apex code that's not touching the db,and doing approx the same number of script statements, don't sweat it, pick the version that's the most understandable/maintainable.

  • Perhaps I should have said that all of this is happening before insert and in no other DML event. Does that make a difference? – DavidSchach Jan 7 '14 at 20:33
  • 1
    No. (plus extra text here so i can post a comment) – superfell Jan 7 '14 at 20:37
  • Thanks for the help. I'm going to combine two of the loops, since it won't make a difference, and will consider rolling in the third - but may not, due to readability concerns. – DavidSchach Jan 7 '14 at 20:41
0

I have written a test class, you can try this in your own org:

    /**
 * Created by nagesingh on 12/4/2017.
 */

@IsTest
private class ProfilingTest {

    private static String TEST_STR = '';

    public static testMethod void testForEachLoop(){
        List<Account> accs = [SELECT Id, Name FROM Account LIMIT 5000];

        for(Account acc : accs){
            acc.Name += TEST_STR;
        }
    }


    public static testMethod void testForLoop(){
        List<Account> accs = [SELECT Id, Name FROM Account LIMIT 5000];

        for(Integer i = 0; i < accs.size(); ++i){
            accs[i].Name += TEST_STR;
        }
    }


    public static testMethod void testImprovedForLoop(){
        List<Account> accs = [SELECT Id, Name FROM Account LIMIT 5000];
        Integer size = accs.size();

        for(Integer i = 0; i < size; ++i){
            accs[i].Name += TEST_STR;
        }
    }


    public static testMethod void testWhileLoop(){
        List<Account> accs = [SELECT Id, Name FROM Account LIMIT 5000];
        Integer size = accs.size();
        Integer i = 0;

        while(i < size){
            accs[i].Name += TEST_STR;
            ++i;
        }
    }
}

Test Result from Illuminated Cloud

This clearly shows that if we use testForLoop, testImprovedForLoop or testWhileLoop, these are 80% much more efficient than testForEachLoop. I think the reason behind this might be that the iteration loop keeps on decreasing when we use one of the three loops defined above. Also as list is an array and the 'i' parameter knows which bucket it need to pick the value from.

This link also may help in performance improvement : https://welkinsuite.com/blog/how-to-get-past-apex-cpu-limits/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.