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...
Ad

More Related Content

What's hot (20)

DotNet programming & Practices
DotNet programming & PracticesDotNet programming & Practices
DotNet programming & Practices
Dev Raj Gautam
 
(Py)testing the Limits of Machine Learning
(Py)testing the Limits of Machine Learning(Py)testing the Limits of Machine Learning
(Py)testing the Limits of Machine Learning
Rebecca Bilbro
 
Escaping the Black Box
Escaping the Black BoxEscaping the Black Box
Escaping the Black Box
Rebecca Bilbro
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
Ted Leung
 
EuroSciPy 2019: Visual diagnostics at scale
EuroSciPy 2019: Visual diagnostics at scaleEuroSciPy 2019: Visual diagnostics at scale
EuroSciPy 2019: Visual diagnostics at scale
Rebecca Bilbro
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
Shine Xavier
 
Xbase - Implementing Domain-Specific Languages for Java
Xbase - Implementing Domain-Specific Languages for JavaXbase - Implementing Domain-Specific Languages for Java
Xbase - Implementing Domain-Specific Languages for Java
meysholdt
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
Michael Heron
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6
BlackRabbitCoder
 
Pattern Matching - at a glance
Pattern Matching - at a glancePattern Matching - at a glance
Pattern Matching - at a glance
Knoldus Inc.
 
Java8 javatime-api
Java8 javatime-apiJava8 javatime-api
Java8 javatime-api
Jini Lee
 
Introduction to Machine Learning with Spark
Introduction to Machine Learning with SparkIntroduction to Machine Learning with Spark
Introduction to Machine Learning with Spark
datamantra
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
Martin Odersky
 
Ae31225230
Ae31225230Ae31225230
Ae31225230
IJERA Editor
 
Introduction to Spark
Introduction to SparkIntroduction to Spark
Introduction to Spark
Sriram Kailasam
 
Simplicitly
SimplicitlySimplicitly
Simplicitly
Martin Odersky
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
 
Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.
Eugene Lazutkin
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
Martin Odersky
 
DotNet programming & Practices
DotNet programming & PracticesDotNet programming & Practices
DotNet programming & Practices
Dev Raj Gautam
 
(Py)testing the Limits of Machine Learning
(Py)testing the Limits of Machine Learning(Py)testing the Limits of Machine Learning
(Py)testing the Limits of Machine Learning
Rebecca Bilbro
 
Escaping the Black Box
Escaping the Black BoxEscaping the Black Box
Escaping the Black Box
Rebecca Bilbro
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
Ted Leung
 
EuroSciPy 2019: Visual diagnostics at scale
EuroSciPy 2019: Visual diagnostics at scaleEuroSciPy 2019: Visual diagnostics at scale
EuroSciPy 2019: Visual diagnostics at scale
Rebecca Bilbro
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
Shine Xavier
 
Xbase - Implementing Domain-Specific Languages for Java
Xbase - Implementing Domain-Specific Languages for JavaXbase - Implementing Domain-Specific Languages for Java
Xbase - Implementing Domain-Specific Languages for Java
meysholdt
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
Michael Heron
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6
BlackRabbitCoder
 
Pattern Matching - at a glance
Pattern Matching - at a glancePattern Matching - at a glance
Pattern Matching - at a glance
Knoldus Inc.
 
Java8 javatime-api
Java8 javatime-apiJava8 javatime-api
Java8 javatime-api
Jini Lee
 
Introduction to Machine Learning with Spark
Introduction to Machine Learning with SparkIntroduction to Machine Learning with Spark
Introduction to Machine Learning with Spark
datamantra
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
Martin Odersky
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
 
Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.Practical pairing of generative programming with functional programming.
Practical pairing of generative programming with functional programming.
Eugene Lazutkin
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
Martin Odersky
 

Similar to Java performance (20)

Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
Daniel Egan
 
EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl
Lyndon White
 
Mobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneMobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, Pune
Bhuvan Khanna
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9
google
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
Togakangaroo
 
Bound and Checked
Bound and CheckedBound and Checked
Bound and Checked
Kevlin Henney
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
Philip Schwarz
 
Designing Better API
Designing Better APIDesigning Better API
Designing Better API
Kaniska Mandal
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Ankit.Rustagi
 
Working Effectively With Legacy Perl Code
Working Effectively With Legacy Perl CodeWorking Effectively With Legacy Perl Code
Working Effectively With Legacy Perl Code
erikmsp
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
dwm042
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
imedo.de
 
Modern C++
Modern C++Modern C++
Modern C++
Richard Thomson
 
Implementation
ImplementationImplementation
Implementation
adil raja
 
Concurrency and parallel in .net
Concurrency and parallel in .netConcurrency and parallel in .net
Concurrency and parallel in .net
Mohammad Hossein Karami
 
C# features
C# featuresC# features
C# features
sagaroceanic11
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
dn
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdf
TobiasGoeschel
 
Salesforce integration questions
Salesforce integration questionsSalesforce integration questions
Salesforce integration questions
Debabrat Rout
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
Daniel Egan
 
EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl
Lyndon White
 
Mobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneMobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, Pune
Bhuvan Khanna
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9
google
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
Togakangaroo
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
Philip Schwarz
 
Working Effectively With Legacy Perl Code
Working Effectively With Legacy Perl CodeWorking Effectively With Legacy Perl Code
Working Effectively With Legacy Perl Code
erikmsp
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
dwm042
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
imedo.de
 
Implementation
ImplementationImplementation
Implementation
adil raja
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
dn
 
Workshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdfWorkshop - The Little Pattern That Could.pdf
Workshop - The Little Pattern That Could.pdf
TobiasGoeschel
 
Salesforce integration questions
Salesforce integration questionsSalesforce integration questions
Salesforce integration questions
Debabrat Rout
 
Ad

Recently uploaded (20)

Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Ad

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...