Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

method calls vs property  RSS feed

 
Peter Kleczka
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to understand some suggestions for optimizing
code running on a mobile device. One of the suggestions
discouraged calling a method in the conditional
part of a for loop. So is there any difference between
a method call and calling a property? That is, in
in the case of an array is the second
loop more effecient than the first?





To be even more anal, is a public class variable
cheeper to call than calling a getter on a private
class variable?

TIA,
Peter (pkleczka@yahoo.com)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
months.length *is not* a method call, *it is* a public final field access.

And yes, a method call *might* be slightly more expensive. In modern JVMs, it would likely be inlined (and therefore not make a difference at all), but I'm not sure about JVMs of mobile devices.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Here is the code I tried to check out



And here is the result as given by eclipse
Package Base Time (seconds)Average Base Time (seconds)<Cumulative Time (seconds)Calls
[-] com.sample.product.util 0.48%0.12%0.48%100.00%
[-] TestLoop 0.48%0.12%0.48%100.00%
main(java.lang.String[]) void0.00%0.00%0.48%25.00%
getName() void 0.34%0.34%0.34%25.00%
test1() void 0.13%0.13%0.13%25.00%
TestLoop() 0.00%0.00%0.00%25.00%
[ September 21, 2006: Message edited by: Rahul Mahindrakar ]
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am not able to format the results properly as HTML is not enabled in this forum.

Anyhow the results state that the getName method takes 34% of the time while the test1 takes only 13% of the time. So it looks like a method call is indeed "bit" costly.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Here are the actual times

PackageBase Time (seconds)
[-] com.sample.product.util0.003029
[-] TestLoop0.003029
main(java.lang.String[]) void0.000012
getName() void0.002162
test1() void0.000849
TestLoop()0.000006
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Mahindrakar:
I am not able to format the results properly as HTML is not enabled in this forum.


Using code tags preserves the formatting.


Anyhow the results state that the getName method takes 34% of the time while the test1 takes only 13% of the time. So it looks like a method call is indeed "bit" costly.


Microbenchmarks such as yours are an art to get meaningful, especially in Java. Make sure that you give the JVM enough warm up time, by putting the whole benchmark into a loop and see how it behaves after the Hot Spot Engine had some time to apply optimizations.

Also play with changing the order in which the two different ways are executed.

You might also want to try whether declaring the getter as final makes a difference.

BTW, benchmark results made with one JRE on one platform could be totally meaningless with a different version on a different platform.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja

Can you help me how I can do this

Make sure that you give the JVM enough warm up time, by putting the whole benchmark into a loop and see how it behaves after the Hot Spot Engine had some time to apply optimizations.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the benchmark in a loop, time each iteration seperately, and make sure that you iterate often enough that the performance stabilizes.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja

Is it possible that you can update my code so that I understand ( in code perspective ) how it needs to be done
 
Ponraj Ram
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Ilja means the following way



But my doubt is,
Is that possible to find out the time constraints, for each call of the methods??[using eclipse or some other tools]

Otherwise you may need to write some piece of code to find out the time factors.

Thanks
-Ponraj
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!