There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Amol Pawar wrote:What are tips to improve any java application performance. I could only come up with following.
- Use Stringbuffer instead of String while concatenating string objects.
To a point its no always quicker - make sure you make constants of your strings so they are not reproduced constantly
- Use localized variables as much as possible
why - they then have to be created and collected all the time
- Always close database and file objects in finally before returning from methods
why? - surely a pool of open connections is faster than opening and closing all the time
- Using Arraylist instead of Vector and Hashmap instead of Hashtable since Vector and Hshtable are synchronized where ArrayList and HashMap are not.
true
Thank you
Amol Pawar
fred rosenberger wrote:you're usually served better by writing clear, readable code tahn by micro-tweaking things to get 'better performance'.
byronc Ozzie wrote:why do people always answer like this when its a forum about performance...
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Amol Pawar wrote:
- Use Stringbuffer instead of String while concatenating string objects.
Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
- Always close database and file objects in finally before returning from methods
why? - surely a pool of open connections is faster than opening and closing all the time
Premature optimization is the root of all evil.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Chris Hurst wrote:Is StringBuilder a such a saver over StringBuffer as Java moves forward
In the specific case, StringBuilder is advertised as not being thread safe, and StringBuffer is thread safe. This is a fundamental difference. On lightly loaded systems with no thread contention, it may be a small difference. On a system/application with lots of lock contention, this can be a huge difference.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Chris Hurst wrote: if you have lock contention you need some form of synchronization somewhere anyway.
so having the software even acquire the lock is wasted effort.
- Brian GoetzI've stated in the past that trying to avoid synchronization is generally a bad idea. The arrival of optimizations such as lock elision adds yet one more reason to avoid trying to eliminate synchronization -- the compiler can do it automatically where it is safe, and leave it in place otherwise.
Because no reference to the Vector ever escapes the getStoogeNames() method, it is necessarily thread-local and therefore any synchronized block that uses it as a lock will have no effect under the JMM. The compiler can inline the calls to the add() and toString() methods, and then it will recognize that it is acquiring and releasing a lock on a thread-local object and can optimize away all four lock-unlock operations.
I've stated in the past that trying to avoid synchronization is generally a bad idea. The arrival of optimizations such as lock elision adds yet one more reason to avoid trying to eliminate synchronization -- the compiler can do it automatically where it is safe, and leave it in place otherwise.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Chris Hurst wrote:I think my point it is theory it doesn't even acquire the lock i.e. no difference ...
...or alternatively don't bother unless you prove you have a performance problem and these small changes would fix it (I've recommend re-reading Fred's response).
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|