Performance Checklist
Most of these suggestions apply only after a bottleneck has been identified:
Include all error-condition checking in blocks guarded by
ifstatements.Avoid throwing exceptions in the normal code path of your application.
Check if a
try-catchin the bottleneck imposes any extra cost.Use
instanceofinstead of making speculative class casts in atry-catchblock.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
longanddoubleinstance 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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access