Performance Checklist

Most of these suggestions apply only after a bottleneck has been identified:

  • Include all error-condition checking in blocks guarded by if statements.

  • Avoid throwing exceptions in the normal code path of your application.

    • Check if a try-catch in the bottleneck imposes any extra cost.

    • Use instanceof instead of making speculative class casts in a try-catch block.

    • Consider throwing exceptions without generating a stack trace by reusing a previously created instance.

    • Include any exceptions generated during the normal flow of the program when running performance tests.

  • Minimize casting.

    • Avoid casts by creating and using type-specific collection classes.

    • Use temporary variables of the cast type, instead of repeatedly casting.

    • Type variables as precisely as possible.

  • Use local variables rather than instance or static variables for faster manipulation.

    • Use temporary variables to manipulate instance variables, static variables, and array elements.

    • Use ints in preference to any other data type.

    • Avoid long and double instance or static variables.

    • Use primitive data types instead of objects for temporary variables.

  • Consider accessing instance variables directly rather than through accessor methods. But note that this breaks encapsulation.

  • Add extra method parameters when that would allow a method to avoid additional method calls.

Get Java Performance Tuning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.