SlideShare a Scribd company logo
Jan 2010
Java Performance
Quest for Speed
Rajesuwer P Singaravelu
Discussed here are subtle
changes that could make
a difference.

Minor gains, of say 2ms,
could be significant if you
are performing 100K
operations to serve a
request.

Architectural patterns are
not considered here.
Introduction
Introduction - Know Your Application
Different applications have different
performance characteristics and bottlenecks.
Performance varies across different hardware,
operating systems, compilers and virtual
machines.
Performance - Cousins
Equally important are:
Code Quality
Maintainability
Readability
Performance - Architecture
Poor performance built into your app?
Heavy use of the object-oriented paradigm (many layers
of superclasses and subclasses)
Simple tuning won t do. Re-design application or parts of
it.
Keep performance in mind when designing your
applications.
Performance - Algorithm
Use efficient algorithms.
Do you use O(n2) bubblesort algorithm or a
much more efficient O(n log n) quicksort ?
Tech Tips
Avoid Creating Objects
Object creation is never free. 

This has gotten better over years, but allocating memory
is always more expensive than not allocating memory.

Reuse objects where possible.
Short Circuit
Perform a cheap test before an expensive one
if (s1 == s2 || s1.equals(s2))
Wrappers
Wrapper classes come with overhead- time and
space
Watch out for autoboxing
Consider Efficient Alternatives:

If you need to add int-s to a list or map, consider
using TIntArrayList, TIntObjectHashMap and such
from trove library.
Prefer Concrete Over Interface
Suppose you have a HashMap object. You can declare it as a
HashMap or as a generic Map:
Map myMap1 = new HashMap() vs HashMap myMap2 = new HashMap()
Which is better?
Calling through an interface reference can take longer than a call
through a concrete reference.
If you have chosen a HashMap because it fits what you're doing, there is little value in calling it a Map.
Given the availability of IDEs that refactor your code for you, there's not much value in calling it a Map
even if you're not sure where the code is headed. (Again, though, public APIs are an exception: a good
API usually trumps small performance concerns.)
Prefer Static Over Virtual
Make methods static

If you don t need access to object s fields
Can be faster

Doesn't require a virtual method table indirection,
so can be faster. 
Also good practice

Method signature conveys that calling the method
can’t/doesn’t alter the object's state.
(Avoid) Internal Getters/Setters
Use
i = this.var 
Instead of 
i = getVar()
Especially inside
loops.
Which is
expensive-
method call or
instance variable
lookup?
Cache Field Lookups
Accessing object
fields is much
slower than
accessing local
variables. 
Instead of:
for (int i=0; i<this.mCount; i++)
dumpItem(this.mItems[i]);
Write:
int count = this.mCount;
Item[] items = this.mItems;
for (int i = 0; i < count; i++)
dumpItems(items[i]);
Enhanced For Loop - Use Caution
For ArrayList, use:
for(int i=0; i < count; i++){ list.get(i); }
instead of:
for(String s: list){ .. }
For other collections the for-each loop will be
equivalent to explicit iterator usage.
Synchronization 
Avoid synchronized methods if you can.
If you can't, synchronizing on methods rather
than on code blocks is slightly faster.
Exception 
Use exceptions where you really need them.
Have a high basic cost, & their presence can
hurt compiler analysis.
Using API classes 
Use Java API classes when they offer native
machine performance that you can't match
using Java. 

For example, arraycopy() is much faster than
using a loop to copy an array of any significant
size.
Avoid expensive constructs
Sometimes Java constructs are so expensive that it can be
worth making your data structures or code a little more
complex to work around the problem. 
For example, you can add a type id number to objects to avoid
paying the cost of an instanceof (this also allows you to use the
result in a switch). 
Similarly, in a long inheritance tree you can avoid casting by
including a method that returns a value of the type you would
otherwise cast to.
Avoid expensive data structures
Expensive Java data structures can be
replaced with simpler ones at the cost of some
extra code complexity.
For example, it can be up to twice as
expensive to access a two-dimensional array
as a one-dimensional array, due to the extra
indirections.
Know your switches
When the numbers are close together, uses a fast
direct lookup.
When the numbers are further apart, uses a slower
search through a table.
This is particularly important if you're trying to
replace a sequence of if statements with a switch.
Method inlining
The Java 2 VM automatically inlines simple
methods at runtime.
Make a method look attractive to the VM to inline
(e.g.: no return value) or manually inline a method if
it doesn't break your object model.
Enums
Enums are very convenient, but unfortunately can be
painful when size and speed matter.
On first use, the class initializer invokes the <init> method
on objects representing each of the enumerated values.
Each object gets its own static field, and the full set is
stored in an array (a static field called "$VALUES"). 
That's a lot of code and data, just for three integers.
Use Package Scope with Inner Classes
Inner class is a totally separate
class (behind the scenes).
To make direct access to
parent class private members,
the compiler generates a
couple of synthetic methods.
The inner-class code calls
these static methods whenever
it needs to access the private
members of enclosing class.
Translation: members accessed
through accessor methods
instead of directly. (accessors are
slower than direct field accesses)
Remedy: declare members
accessed by inner classes to
have package scope, rather than
private scope.
Flip-side: other classes in the
same package can access too;
runs counter to the standard OO
practice.
Replacing API classes
Sometimes API classes do more than you
need -with a corresponding increase in
execution time
You can write specialized versions that do less
but run faster.
Overriding API methods
Performance problems with a Java API method?
Define a subclass, override that method with your
own (hopefully more efficient) version.
Strength Reduction
Use cheaper operations in place of expensive ones.
‣ += instead of ...=...+... result in fewer byte code instructions.
‣ Make a 1D array variable that points to the row, if repeatedly
accessing elements in a single row of a 2D array
‣ Shifts instead of multiplication by powers of two 
‣ Multiplication instead of exponentiation, etc.
/ mathematical optimizations of this type generally have little benefit
unless you are using a just-in-time compiler /
Variable allocation
For desperate optimizers only. 
The first four numeric variables or arguments in a method are accessed
using via shorter bytecode instructions, although only three are usable in
non-static methods. 
If you declare your most frequently-used variables first (e.g., loop indices),
the inner bytecode loops of your methods will be marginally shorter and
possibly faster. Note that although the number of bytecodes for the inner
loop is the same, the length of the bytecode has decreased.
Common subexpression elimination
If an expensive expression (for example, the result of a method
call) is used more than once within a block of code, calculate it
once and put it into a temporary variable for subsequent reuse. 
double d = a * Math.sqrt(c); double tmp = Math.sqrt(c);
double e = b * Math.sqrt(c); double d = a * tmp;
double e = b * tmp;
Loop invariant code motion
If an expression inside a loop doesn't change after the
loop is entered (i.e. it's invariant), calculate the value of
the expression outside the loop and assign it to a
temporary variable. Then use the temporary variable
within the loop. 
Note that we could also treat array.length as a loop
invariant.
Use the right algorithms & data structures
Don't use an O(n2) bubblesort algorithm to sort a
thousand elements when there's an O(n log n) quicksort
available. 
Similarly, don't store a thousand items in an array that
requires an O(n) search when you could use an O(log n)
binary tree, or an O(1) Java hash table.
Wholesale is Cheaper
Perform operations in bulk instead of one at a
time.
DB access in loop: Optimize SQL to perform in
one bulk access (not same as executeBatch() )
Closing Notes
To write good, efficient code:

Understand what the code you write really does. 
Make deliberate choice, not inadvertent side effect:

If you really want to allocate an iterator, by all means use
enhanced for loop syntax on a List; just make it a deliberate
choice.
Build performance into your application; then make it better.
Always think carefully about what your code is doing, and be on
the lookout for ways to speed it up.
quest for speed continues...

More Related Content

PPT
An introduction to javascript
PPTX
C traps and pitfalls for C++ programmers
ODP
Best practices in Java
ODP
Functors, Applicatives and Monads In Scala
PPT
Of Lambdas and LINQ
PPTX
More Little Wonders of C#/.NET
PPT
Oo Design And Patterns
PDF
Yellowbrick: Steering machine learning with visual transformers
An introduction to javascript
C traps and pitfalls for C++ programmers
Best practices in Java
Functors, Applicatives and Monads In Scala
Of Lambdas and LINQ
More Little Wonders of C#/.NET
Oo Design And Patterns
Yellowbrick: Steering machine learning with visual transformers

What's hot (20)

PPTX
DotNet programming & Practices
PDF
(Py)testing the Limits of Machine Learning
PPTX
Escaping the Black Box
PDF
A Survey of Concurrency Constructs
PDF
EuroSciPy 2019: Visual diagnostics at scale
PPTX
Fuel Up JavaScript with Functional Programming
KEY
Xbase - Implementing Domain-Specific Languages for Java
PPTX
2CPP04 - Objects and Classes
PPTX
The Little Wonders of C# 6
ODP
Pattern Matching - at a glance
PPTX
Java8 javatime-api
PDF
Introduction to Machine Learning with Spark
PDF
What To Leave Implicit
PDF
Ae31225230
PDF
Introduction to Spark
PDF
Simplicitly
ODP
Functional programming with Scala
PDF
Practical pairing of generative programming with functional programming.
PPTX
Constructors & Destructors
PPTX
What To Leave Implicit
DotNet programming & Practices
(Py)testing the Limits of Machine Learning
Escaping the Black Box
A Survey of Concurrency Constructs
EuroSciPy 2019: Visual diagnostics at scale
Fuel Up JavaScript with Functional Programming
Xbase - Implementing Domain-Specific Languages for Java
2CPP04 - Objects and Classes
The Little Wonders of C# 6
Pattern Matching - at a glance
Java8 javatime-api
Introduction to Machine Learning with Spark
What To Leave Implicit
Ae31225230
Introduction to Spark
Simplicitly
Functional programming with Scala
Practical pairing of generative programming with functional programming.
Constructors & Destructors
What To Leave Implicit
Ad

Similar to Java performance (20)

PPT
JAVA Tutorial- Do's and Don'ts of Java programming
PPT
JAVA Tutorial- Do's and Don'ts of Java programming
PDF
Mobile Developer Summit 2012, Pune
PPTX
Java best practices
PPTX
The programming philosophy of jrql
PDF
Android App Performance
PDF
Clean code
PPT
Java programing considering performance
PDF
Java Performance Tuning
PDF
Java beginners meetup: Introduction to class and application design
PPTX
Java best practices
PDF
PPTX
Effective Java
PPT
Refactoring - improving the smell of your code
PDF
How can JAVA Performance tuning speed up applications.pdf
PPT
packages and interfaces
PDF
API Design
PDF
Object Oriented Best Practices - Summary
PPTX
Object-oriented programming
ODT
Designing Better API
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
Mobile Developer Summit 2012, Pune
Java best practices
The programming philosophy of jrql
Android App Performance
Clean code
Java programing considering performance
Java Performance Tuning
Java beginners meetup: Introduction to class and application design
Java best practices
Effective Java
Refactoring - improving the smell of your code
How can JAVA Performance tuning speed up applications.pdf
packages and interfaces
API Design
Object Oriented Best Practices - Summary
Object-oriented programming
Designing Better API
Ad

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Chapter 3 Spatial Domain Image Processing.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
Programs and apps: productivity, graphics, security and other tools
sap open course for s4hana steps from ECC to s4
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Unlocking AI with Model Context Protocol (MCP)
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
MIND Revenue Release Quarter 2 2025 Press Release
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Java performance

  • 1. Jan 2010 Java Performance Quest for Speed Rajesuwer P Singaravelu
  • 2. Discussed here are subtle changes that could make a difference. Minor gains, of say 2ms, could be significant if you are performing 100K operations to serve a request. Architectural patterns are not considered here. Introduction
  • 3. Introduction - Know Your Application Different applications have different performance characteristics and bottlenecks. Performance varies across different hardware, operating systems, compilers and virtual machines.
  • 4. Performance - Cousins Equally important are: Code Quality Maintainability Readability
  • 5. Performance - Architecture Poor performance built into your app? Heavy use of the object-oriented paradigm (many layers of superclasses and subclasses) Simple tuning won t do. Re-design application or parts of it. Keep performance in mind when designing your applications.
  • 6. Performance - Algorithm Use efficient algorithms. Do you use O(n2) bubblesort algorithm or a much more efficient O(n log n) quicksort ?
  • 8. Avoid Creating Objects Object creation is never free. 
 This has gotten better over years, but allocating memory is always more expensive than not allocating memory. Reuse objects where possible.
  • 9. Short Circuit Perform a cheap test before an expensive one if (s1 == s2 || s1.equals(s2))
  • 10. Wrappers Wrapper classes come with overhead- time and space Watch out for autoboxing Consider Efficient Alternatives:
 If you need to add int-s to a list or map, consider using TIntArrayList, TIntObjectHashMap and such from trove library.
  • 11. Prefer Concrete Over Interface Suppose you have a HashMap object. You can declare it as a HashMap or as a generic Map: Map myMap1 = new HashMap() vs HashMap myMap2 = new HashMap() Which is better? Calling through an interface reference can take longer than a call through a concrete reference. If you have chosen a HashMap because it fits what you're doing, there is little value in calling it a Map. Given the availability of IDEs that refactor your code for you, there's not much value in calling it a Map even if you're not sure where the code is headed. (Again, though, public APIs are an exception: a good API usually trumps small performance concerns.)
  • 12. Prefer Static Over Virtual Make methods static
 If you don t need access to object s fields Can be faster
 Doesn't require a virtual method table indirection, so can be faster. Also good practice
 Method signature conveys that calling the method can’t/doesn’t alter the object's state.
  • 13. (Avoid) Internal Getters/Setters Use i = this.var Instead of i = getVar() Especially inside loops. Which is expensive- method call or instance variable lookup?
  • 14. Cache Field Lookups Accessing object fields is much slower than accessing local variables. Instead of: for (int i=0; i<this.mCount; i++) dumpItem(this.mItems[i]); Write: int count = this.mCount; Item[] items = this.mItems; for (int i = 0; i < count; i++) dumpItems(items[i]);
  • 15. Enhanced For Loop - Use Caution For ArrayList, use: for(int i=0; i < count; i++){ list.get(i); } instead of: for(String s: list){ .. } For other collections the for-each loop will be equivalent to explicit iterator usage.
  • 16. Synchronization Avoid synchronized methods if you can. If you can't, synchronizing on methods rather than on code blocks is slightly faster.
  • 17. Exception Use exceptions where you really need them. Have a high basic cost, & their presence can hurt compiler analysis.
  • 18. Using API classes Use Java API classes when they offer native machine performance that you can't match using Java. For example, arraycopy() is much faster than using a loop to copy an array of any significant size.
  • 19. Avoid expensive constructs Sometimes Java constructs are so expensive that it can be worth making your data structures or code a little more complex to work around the problem. For example, you can add a type id number to objects to avoid paying the cost of an instanceof (this also allows you to use the result in a switch). Similarly, in a long inheritance tree you can avoid casting by including a method that returns a value of the type you would otherwise cast to.
  • 20. Avoid expensive data structures Expensive Java data structures can be replaced with simpler ones at the cost of some extra code complexity. For example, it can be up to twice as expensive to access a two-dimensional array as a one-dimensional array, due to the extra indirections.
  • 21. Know your switches When the numbers are close together, uses a fast direct lookup. When the numbers are further apart, uses a slower search through a table. This is particularly important if you're trying to replace a sequence of if statements with a switch.
  • 22. Method inlining The Java 2 VM automatically inlines simple methods at runtime. Make a method look attractive to the VM to inline (e.g.: no return value) or manually inline a method if it doesn't break your object model.
  • 23. Enums Enums are very convenient, but unfortunately can be painful when size and speed matter. On first use, the class initializer invokes the <init> method on objects representing each of the enumerated values. Each object gets its own static field, and the full set is stored in an array (a static field called "$VALUES"). That's a lot of code and data, just for three integers.
  • 24. Use Package Scope with Inner Classes Inner class is a totally separate class (behind the scenes). To make direct access to parent class private members, the compiler generates a couple of synthetic methods. The inner-class code calls these static methods whenever it needs to access the private members of enclosing class. Translation: members accessed through accessor methods instead of directly. (accessors are slower than direct field accesses) Remedy: declare members accessed by inner classes to have package scope, rather than private scope. Flip-side: other classes in the same package can access too; runs counter to the standard OO practice.
  • 25. Replacing API classes Sometimes API classes do more than you need -with a corresponding increase in execution time You can write specialized versions that do less but run faster.
  • 26. Overriding API methods Performance problems with a Java API method? Define a subclass, override that method with your own (hopefully more efficient) version.
  • 27. Strength Reduction Use cheaper operations in place of expensive ones. ‣ += instead of ...=...+... result in fewer byte code instructions. ‣ Make a 1D array variable that points to the row, if repeatedly accessing elements in a single row of a 2D array ‣ Shifts instead of multiplication by powers of two ‣ Multiplication instead of exponentiation, etc. / mathematical optimizations of this type generally have little benefit unless you are using a just-in-time compiler /
  • 28. Variable allocation For desperate optimizers only. The first four numeric variables or arguments in a method are accessed using via shorter bytecode instructions, although only three are usable in non-static methods. If you declare your most frequently-used variables first (e.g., loop indices), the inner bytecode loops of your methods will be marginally shorter and possibly faster. Note that although the number of bytecodes for the inner loop is the same, the length of the bytecode has decreased.
  • 29. Common subexpression elimination If an expensive expression (for example, the result of a method call) is used more than once within a block of code, calculate it once and put it into a temporary variable for subsequent reuse. double d = a * Math.sqrt(c); double tmp = Math.sqrt(c); double e = b * Math.sqrt(c); double d = a * tmp; double e = b * tmp;
  • 30. Loop invariant code motion If an expression inside a loop doesn't change after the loop is entered (i.e. it's invariant), calculate the value of the expression outside the loop and assign it to a temporary variable. Then use the temporary variable within the loop. Note that we could also treat array.length as a loop invariant.
  • 31. Use the right algorithms & data structures Don't use an O(n2) bubblesort algorithm to sort a thousand elements when there's an O(n log n) quicksort available. Similarly, don't store a thousand items in an array that requires an O(n) search when you could use an O(log n) binary tree, or an O(1) Java hash table.
  • 32. Wholesale is Cheaper Perform operations in bulk instead of one at a time. DB access in loop: Optimize SQL to perform in one bulk access (not same as executeBatch() )
  • 33. Closing Notes To write good, efficient code:
 Understand what the code you write really does. Make deliberate choice, not inadvertent side effect:
 If you really want to allocate an iterator, by all means use enhanced for loop syntax on a List; just make it a deliberate choice. Build performance into your application; then make it better. Always think carefully about what your code is doing, and be on the lookout for ways to speed it up.
  • 34. quest for speed continues...