Use these techniques for performance improvements:
Move loop invariants outside the loop.
Make the tests as simple as possible.
Use only local variables inside a loop; assign class fields to local variables before the loop.
Move constant conditionals outside loops.
Combine similar loops.
If loops are interchangeable, nest the busiest one.
As a last resort, unroll the loop.
expr
to Table LookupWhen a value is being selected based on a single expression with a range of small integers, convert it to a table lookup. Conditional branches defeat many compiler optimizations.
Though caching takes more memory, it can be used for performance improvement. Use the technique of caching values that are expensive to fetch or compute.
Increase performance by precomputing values known at compile time.
Save startup time by delaying computation of results until they are needed.
Speed performance up by putting all one-time initializations into a class initializer.