• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to determine which is better?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I figure out which is more efficient (memory/cpu)? I'd like a way to *prove* which option is more optimal. Do I keep executing a bean.getSomething() or assign it to a local variable? Note that bean.getSomething() just returns a property's value (no backend processing).

Thanks!

ron

Option 1:


OR

Option 2:
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that code is executed often enough, the hotspot engine might well decide to inline the method call. So the performance characteristic will likely be near to indistinguishable. Worrying about it is likely to be nothing more than a waste of time...
 
Ron Hamilton
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response.

Perhaps it may seem like a waste of time. Certainly for a few method calls any performance degradation may not be noticed.

But what if that getter were being called 10 times? 100 times? X times?

I'm wondering if anyone knows of a way to prove this.

Thanks!

ron
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Microtuning like this also depends on the jdk version, jvm, and the mode (such as hotspot) that you are running in and probably many other factors. Rarely/never will such code be a bottleneck, and so most seasoned developers don't know or care about the performance difference.

If the object you are calling is a shared object calling getSomething() multiple times may return a different answer each time you call it. If possible I like to design objects to be immutable so I don't have to worry about such things. Assigning to a temporary variable would ensure that the value isn't changed underneath you (that assumes this would be a bad thing). As always design considerations such as these should take a front seat to performance.

If it is important to find out the difference you should use a profiling tool. You can learn about them at www.javaperformancetuning.com
[ January 18, 2007: Message edited by: steve souza ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm wondering if anyone knows of a way to prove this.


The simplest and most straightforward way would be to write a small test program which executes the statement 10 times, 100 times or X times and measure how long it takes for the program to execute. You could use a profiler to measure execution time, memory usage, garbage collection etc.

I agree with Ilja that micro-optimizations like this almost always do not make any significant difference. If you want to tune your program for performance, you should start by measuring the program's performance to find out where the bottleneck is and concentrate on that instead of trying to do random micro-optimizations.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ron Hamilton:
But what if that getter were being called 10 times? 100 times?



The time it takes to call a getter a hundred times will be so small that you would have problems measuring it.

X times?



No matter how often the getter is called, it's very likely that the real bottleneck is in whatever the doSomething methods do.

Most often, time is better invested in finding a better algorithm (for example one that decreases X significantly).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic