Top 10 Easy Performance Optimisations in Java - PowerPoint PPT Presentation

About This Presentation
Title:

Top 10 Easy Performance Optimisations in Java

Description:

Best core java training in Bangalore. • Students get Live Project to practice • Highly talented with 10+ Years Experienced Trainer • Well Equipped Class Rooms and Lab Facility • Java J2EE Sun Certification Guidance Support with Exam Dumps • Java Fast Track course available with best Fees • Resume & Interviews Preparation Support Visit and make an enquiry to get a call back with much more information. Contact 9738001024 and attend demo. – PowerPoint PPT presentation

Number of Views:23

less

Transcript and Presenter's Notes

Title: Top 10 Easy Performance Optimisations in Java


1
Top 10 Easy Performance Optimisations in Java
CALL 9738001024
2
1. Use StringBuilder
This should be your default in almost all Java
code. Try to avoid the operator. may argue that
it is just syntax sugar for a StringBuilder
anyway, as in String x "a" args.length
"b" But what happens, if later on, you need to
amend your String with optional parts? String x
"a" args.length "b"   if (args.length
1)     x x args0
3
You will now have a second StringBuilder, that
just needlessly consumes memory off your heap,
putting pressure on your GC. Write this
instead StringBuilder x new
StringBuilder("a") x.append(args.length) x.appen
d("b")   if (args.length 1)     x.append(args
0)
4
2. Avoid regular expressions
Regular expressions are
relatively cheap and convenient. But if youre in
the N.O.P.E. branch, theyre about the worst
thing you can do. If you absolutely must use
regular expressions in computation-intensive code
sections, at least cache the Pattern reference
instead of compiling it afresh all the time
5
3. Do not use iterator()
Now, this advice is really not for
general use-cases, but only applicable deep down
in a N.O.P.E. branch. Nonetheless, you should
think about it. Writing Java-5 style foreach
loops is convenient. You can just completely
forget about looping internals, and write
6
4. Dont call that method
Some methods are simple
expensive. In our N.O.P.E. branch example, we
dont have such a method at the leaf, but you may
well have one. Lets assume your JDBC driver
needs to go through incredible trouble to
calculate the value of ResultSet.wasNull(). Your
homegrown SQL framework code might look like this
7
5. Use primitives and the stack
The above example is from jOOQ, which uses a lot
of generics, and thus is forced to use wrapper
types for byte, short, int, and long at least
before generics will be specialisable in Java 10
and project Valhalla. But you may not have this
constraint in your code, so you should take all
measures to replace
8
6. Avoid recursion
Modern functional
programming languages like Scala encourage the
use of recursion, as they offer means of
optimising tail-recursing algorithms back into
iterative ones. If your language supports such
optimisations, you might be fine. But even then,
the slightest change of algorithm might produce a
branch that prevents your recursion from being
tail-recursive. Hopefully the compiler will
detect this! Otherwise, you might be wasting a
lot of stack frames for something that might have
been implemented using only a few local variables.
9
7. Use entrySet()
When you want to iterate
through a Map, and you need both keys and values,
you must have a very good reason to write the
following
10
8. Use EnumSet or EnumMap
There are some cases where
the number of possible keys in a map is known in
advance for instance when using a configuration
map. If that number is relatively small, you
should really consider using EnumSet or EnumMap,
instead of regular HashSet or HashMap instead.
This is easily explained by looking at
EnumMap.put()
11
9. Optimise your hashCode() and equals() methods
If you cannot use an
EnumMap, at least optimise your hashCode() and
equals() methods. A good hashCode() method is
essential because it will prevent further calls
to the much more expensive equals() as it will
produce more distinct hash buckets per set of
instances.
12
10. Think in sets, not in individual elements
Last but not least, there
is a thing that is not Java-related but applies
to any language. Besides, were leaving the
N.O.P.E. branch as this advice might just help
you move from O(N3) to O(n log n), or something
like that.
13
VISIT WWW.INFOCAMPUS.CO.IN
CALL 9738001024
14
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com