Apex Speed Test – Does System.Debug() cost CPU limit?

Reading Time: 3 minutes

CPU Limit Experiment

Apex Speed Test Experiment:
This blog is part of Apex Speed Test Experiments.
These experiments check that are we doing something wrong while writing code? What thing is good to write or what is not? Is your single line of code killing the CPU limit?
I am doing experiments whatever I found. You are most welcome here, If you have anything which can be added in these experiments
#CodeShouldNotEatCPU #CPUScientist

Our Experiment:
In this experiment, we will check that if system.debug are eating your CPU time or not.

You Will Need:
A computer, a browser, a Salesforce Org and 2000 lead records.

Let’s Start:
We have taken 2000 lead records for this experiment. We are querying these records and iterating through a loop. Create this class in your developer org.

Experiment 1:
We are not using any debugs in this experiment. Open the developer console. Now Open Execute Anonymous Window. (This option present in Debug menu.)
Copy this code ApexSpeedExperiment_1.runExperiment1(); and run it from developer console. I called this method 5 times and here are the results.

Speed Test Exp 1

Result: Average time taken is 29.2 Milliseconds.

Experiment 2:
We are using system debugs in this experiment. Open the developer console. Now Open Execute Anonymous Window. (This option present in Debug menu.)
Copy this code ApexSpeedExperiment_1.runExperiment2(); and run it from developer console. I called this method 5 times and here are the results.

Speed Test Exp 2

Result: Average time taken is 110 Milliseconds.

Conclusion:
According to our experiments, System.debug consume CPU limit.
1. It is good that we should use needed debugs in production.
2. Don’t use debugs for API response. As responses of APIs can be too large.
3. Don’t print the whole record instance in debugging.
4. If large debugs are needed then use a custom setting in production to print debugs. Exp:
if( isDebugOn ){
system.debug(‘Your Debug’);
}
5. Remove unwanted debugs before deploying the code to production.

Note: If logging is turned off, the call still happens. If any line of code that is written in the code will be executed and it will get count against execution time.
According to Salesforce Developer Document:
If the msg argument is not a string, the debug method calls String.valueOf to convert it into a string. The String.valueOf method calls the toString method on the argument, if available, or any overridden toString method if the argument is a user-defined type. Otherwise, if no toString method is available, it returns a string representation of the argument. If the log level for Apex Code is set to DEBUG or higher, the message of this debug statement will be written to the debug log.
This above process consumes the CPU time.

Thank You for taking part in the Apex Speed Test Experiment. If you have any other experiment in mind. Please let me know. I will conduct an experiment and will share the blog here.

2 thoughts on “Apex Speed Test – Does System.Debug() cost CPU limit?

  • November 10, 2018 at 8:20 am
    Permalink

    If you enable debug log thats when the system.debug is enabled isnt it?

    Reply
    • November 10, 2018 at 11:31 am
      Permalink

      If logging is turned off, the call still happens. If any line of code that is written in the code will be executed and it will get count against execution time.

      Reply

Leave a Reply