My laptop is 300 times faster

In Relative performance of CRUD, while the delete/insert time dominated, the calculation part took more time than I expected too. I had been thinking that it was the governor limits that stopped you from using up too many CPU cycles and that up to those limits you would get pretty high performance, but that did not appear to be the case.

To separate out the CPU part, I created the very basic benchmark below in both Java and Apex. I ran the Java on my 2 year-old laptop (14ms on average) and the Apex on Na7 (5064ms on average). Hence the conclusion that my laptop is 300 times faster. Now I just have to figure out how to get my laptop to scale to a million or so users…

But the lesson also is to be realistic about the performance of any CPU cycle intensive logic in your application. Even for your first user, it is going to be slower than you probably expected.

Java benchmark:

public class Calc {

    public static void main(String[] args) {

        long t1 = System.currentTimeMillis();
        Integer result = calc(200);
        long t2 = System.currentTimeMillis();

        System.out.println("result=" + result);
        System.out.println("ms=" + (t2 - t1));
    }

    private static Integer calc(Integer n) {
        Integer sum = 0;
        for (Integer i = 0; i < n; i++) {
            for (Integer j = 0; j < n; j++) {
                sum += i * j;
            }
        }
        return sum;
    }
}

Apex benchmark:

@isTest
private class CalcTest {

    @isTest
    static void test() {
        System.debug(LoggingLevel.ERROR);

        Long t1 = Datetime.now().getTime();
        Integer result = calc(200);
        Long t2 = Datetime.now().getTime();

        System.debug(LoggingLevel.ERROR, 'result=' + result);
        System.debug(LoggingLevel.ERROR, 'ms=' + (t2 - t1));
    }

    private static Integer calc(Integer n) {
        Integer sum = 0;
        for (Integer i = 0; i < n; i++) {
            for (Integer j = 0; j < n; j++) {
                sum += i * j;
            }
        }
        return sum;
    }
}

1 thought on “My laptop is 300 times faster

  1. Pingback: Parsing CSV while staying within the “total number of executed script statements” governor limit « Force 201 – Force.com Development

Leave a comment