Apex code benchmarking
Amit Chaudhary

Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates. He is a active blogger and founder of Apex Hours.

Apex Code Benchmarking | CPU time Limit

Benchmarking your own code is art. Each single line of code written that makes difference so Each developer should know how effiective his code is working. This session will help you to learn benchmarking technique and help you to build more robust and elastic application. It’s About CPU Time.


  1. Cost of Code
  2. CPU time Limit
  3. Benchmarking technique for Code
  4. Benchmarking technique for Declarative
  5. Demo

Speaker           :  Purushottam Bhaigade
Date                  :  Saturday, DEC 07, 2019 10:00 AM EST ( 8:30 PM IST)



Before Winter’14

  1. Script limit – Total number of line of Apex code executed
  2. Separate limits for each Managed Package (Aloha)
  3. Trigger Context – 200,000 lines
  4. Async (Batch & Future) – 1,000,000 lines
  5. Workflow, Process Builders, Formulas has no limits at all

What Changed after Winter’14 ?

Script Limit were changed to calculate CPU Time usage

Why Software Benchmarking is hard?

  1. Time measurement can’t trusted always, because system performance varies based on time of day and momentary load.
  2. Debug log cost CPU time
  3. You don’t have control on background operations like garbage collection
  4. Result might change with each patch release


  1. Assignment from static field reference is 20x time slower than variable assignment
  2. Assignment from dynamic field reference is 30x time slower than static assignment
  3. Use temporary variable instead of referencing a field multiple times!
  4. The cost of sending email notifications via Workflows can add up 15 ms/email (10 field merge
    template). Ultimately that is 3 seconds for 200 records !
  5. What is Alternative then? Create task for user – only 0.5 ms per record (tested with basic formula workflow)
  6. Validation Rules : Each validation formula function takes about 30 microseconds. Example : 10 validation rules, each with 10 functions, applied to 200 records = 600 ms

How to prevent this?

  1. Use Limits.getCPUTime() to find out you are getting into trouble, then either exit or go async if you are getting close
  2. Learn more advance architectures to detect reentrancy
  3. Try to use Platform event in your solution for Async operation
  4. Accept that you still may get the blame


Here is recording of session:-



Here is PPT of session:-

Apex code Benchmarking from Amit Chaudhary


Please note that we have limit of 200 attendees that can join the online sessions. However, recording will be posted on our YouTube channel. Make sure to subscribe our YouTube channel to get notification for video upload.

Check our “Session in 2019” page for all upcoming and old sessions of 2019.

Sharing is Caring so Share with your friends


Salesforce Apex Hours


Share this article

Leave a reply

Keep in Touch

Subscribe for Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,229 other subscribers


Our Supporter


Apex Hours

Apex Hours is one stop platform to learn Salesforce skills and technology

Join our Newsletter and get tips and tricks how to explore the salesforce for free!