You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Efficient Way of Using for Loop to Reduce CPU Process Time in Salesforce


By: Radha

Introduction

Governor limits in Salesforce is one of the key considerations you need to follow while designingSOQL queries, DML statements, and etc in order to run applications without any issue. This article will help reduce CPU execution time–while processing records inside a “for” loop–to avoid reaching the CPU limit enforced by the Governor.

Processing Records in for Loop

Consider that you are going to access less than or equal to 50000 records and process these records in a loop. While processing these records in a “for” loop, the execution time will be longer depending upon your way of processing records. In order to achieve faster performance, you need to use a right approach in the “for” loop. Let us go through these four examples to understand how each approach takes different amount of execution time:

Example Code

public class LoopConsideration

{

public static void method1()

{

List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 50000];

for(Integer i=0; i<accounts.Size(); i++)

{

accounts[i].Name = accounts[i].Name + 'Go';

}

}

public static void method2()

{

List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 50000];

for(Account currAccount : accounts)

{

currAccount.Name = currAccount.Name + 'Go';

}

}

public static void method3()

{

for(Account currAccount : [SELECT Id, Name FROM Account LIMIT 50000])

{

currAccount.Name = currAccount.Name + 'Go';

}

}

public static void method4()

{

List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 50000];

for(Integer i=0, j= accounts.Size(); i < j; i++)

{

accounts[i].Name = accounts[i].Name + 'Go';

}

}

}

 

Run the above four methods using Developer Console to see the difference

Integer startTime = Limits.getCpuTime();

LoopConsideration.method1();

Integer endTime = Limits.getCpuTime();

System.debug('Spent Time'+ (endTime - startTime));

Similarly, run remaining three methods by changing method name from LoopConsideration.method1() to LoopConsideration.method2(), LoopConsideration.method3(), and LoopConsideration.method4()

 

Here is the processing time for all four methods. For these examples, I have used 10000 records. Based on this record count, the process times are shown below:

Execution Time:

Method 1: Spent Time: 647

Method 2: Spent Time: 444

Method 3: Spent Time: 1558

Method 4: Spent Time: 368

 

Conclusion

The first three methods will check list size in each iteration and iterate components one by one, leading to more CPU consumption. So if you assign the list size to a variable, then processing records using that variable will take less time. Therefore; the method4() approach in the above class will reduce the CPU consumption.

 

Reference

Apex Code Best Practices