aspose file tools*
The moose likes Performance and the fly likes How to determine which is better? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "How to determine which is better?" Watch "How to determine which is better?" New topic
Author

How to determine which is better?

Ron Hamilton
Greenhorn

Joined: Sep 10, 2002
Posts: 16
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:
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ron Hamilton
Greenhorn

Joined: Sep 10, 2002
Posts: 16
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
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
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 ]

http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to determine which is better?