Efficiency in Java

S

sara

Hi All,

I am working on a project and I want it to be as fast and efficient as
possible. Do you know any website or book which talks about hot to make
programs fast by using different data structures?
e.g. using an array of strings instead of vector of objects and then
convert object to string.

Thanks.
 
S

Stefan Ram

sara said:
I am working on a project and I want it to be as fast and efficient as
possible. Do you know any website or book which talks about hot to make
programs fast by using different data structures?

Optimization might not be /that/ evil as it sometimes is said.

After all, some products have as their main problem that they
are too slow.

Also, some early design decisions might have a critical impact
on the speed, so that even early optimization might not be
that bad.

However, the problem might be the idea to teach or learn
optimization in isolation from other aspects of programming,
Java and computer science.

If someone is well educated in computer science (e.g.,
algorithms and complexity), in Java and the JVM and software
concepts (patterns, paradigms) and tools (like profilers) he
will also have learnt what makes software slow and what makes
it fast. He will not need a separate education on the topic.

However, when someone would try to apply a toolbox of
speedup-tricks in isolation from other aspects of software
quality and trade-offs with them, it might have the opposite
effect or could endager the correctness of the software.

Additional problems are that tricks or strategies that speed
up a Java program might slow it down on a later version of the
JVM or on another JVM than that of Sun Microsystems, Inc.

An important aspect of runtime efficency might be
memory-locality, e.g., execution of code that already is in
the cache of the processor is faster that execution of code
that needs to be read from RAM. Or the success of branch
prediction. But the more abstract and high-level the language
is, the more difficult it might be for the programmer to
foresee this.

This might be somewhat out-of-date:

http://www.cybergrain.com/tech/writing/efficient-java.html

Then there also is

http://java.sun.com/docs/books/effective/

and

http://java.sun.com/docs/hotspot/PerformanceFAQ.html
 
N

Nick Vatamaniuc

Sara,

Here is what I would do:

1. Pick the right algorithm

2. Write the _correct_ code first, profile, identify the speed related
critical section and optimize that. The "premature optimization is the
root of all evil" cliche is actually quite true.

3. When you optimize the _critical section_ consider these:
3a. Use built-in data types over classes
3b. Aviod creating too many class instances.
3c. Prefer unsynchronized data structures over synchronized when
possible
3d. Play with the garbage collector (disable, run manually, use an
alternative one etc.)
3e. If data structures are immutable, declare them as final
3f. Move invariant code outside the critical section if possible
3g. static methods can be faster
These things might or might not help based on your specific domain...

Note: Doing too much on #3 there is a risk at turning your code into a
mess. Always start with and focus on #1.

Hope this helps,

Nick Vatamaniuc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top