Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does method call affect the performance in Java?

I do like to write really readable code in Java. For that I use the builder pattern and have a lot of static methods. But at some point I do call or chain a lot of methods, so I came up with this question: Does it have any performance issues calling a lot of methods?

like image 370
why Avatar asked Aug 08 '11 17:08

why


2 Answers

What the method does has a far greater effect than the method call itself. This is a pre-mature optimization that you would be advised to avoid.

You have no idea how runtime optimizations built into the JVM itself will affect performance. They will be much smarter than your attempts.

Write your app, profile it, and see if there are any obvious improvements you can make. Repeat until performance is acceptable.

like image 51
duffymo Avatar answered Oct 11 '22 13:10

duffymo


Readability is almost always the way to go. There is impact on performance for function calls but non readable code will probably suffer from other implementation issues that will have higher performance hit.

Use the keyword final where appropriate so that you suggest inlineing for these functions.

A situation where you don't want to use a lot of function calls are tight loops. Something that happens hundred of thousands of times. And usually these places can be optimized at the end if optimizing the software is needed.

like image 35
Aleks Avatar answered Oct 11 '22 14:10

Aleks