jQuery in Action, 2nd edition*
The moose likes Performance and the fly likes which is more perfomant? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "which is more perfomant?" Watch "which is more perfomant?" New topic
Author

which is more perfomant?

sebastian aguirre
Greenhorn

Joined: Sep 14, 2011
Posts: 3
hi.
which of these two possibilities do you recommend performance wise and why?
choices are

1
--------------------------
....
this.setPropertyA(getMyObject().getPropertyA())
this.setPropertyB(getMyObject().getPropertyB())
this.setPropertyC(getMyObject().getPropertyC())
this.setPropertyD(getMyObject().getPropertyD())
....
------------------------------

2-
------------------------------
....
MyObject myObject = getMyObject();
this.setPropertyA(myObject.getPropertyA())
this.setPropertyB(myObject.getPropertyB())
this.setPropertyC(myObject.getPropertyC())
this.setPropertyD(myObject.getPropertyD())
....
------------------------------

thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41053
    
  43
The 2nd approach should be faster, but whether that makes a noticeable difference depends on how complex the getMyObject method is.


Ping & DNS - my free Android networking tools app
sebastian aguirre
Greenhorn

Joined: Sep 14, 2011
Posts: 3
Ulf Dittmer wrote:The 2nd approach should be faster, but whether that makes a noticeable difference depends on how complex the getMyObject method is.


visually I like more the getter.
as for performance I don't know.
I guess the get takes more time since it's a method.
but the variable is something that is created and remains more time occupying memory so I'm not sure.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

To compare the two versions you must assume that "getMyObject()" returns the same object every time you call it. If that isn't the case then the two versions don't necessarily do the same thing.
sebastian aguirre
Greenhorn

Joined: Sep 14, 2011
Posts: 3
Paul Clapham wrote:To compare the two versions you must assume that "getMyObject()" returns the same object every time you call it. If that isn't the case then the two versions don't necessarily do the same thing.


yep, I'm assuming that....
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

We had this discussion about this on a performance lecture I held. There are no significant difference.

Our test program did the same test 100.000 times on the "get" approach and then on the "by reference" approach. Running this a couple of times on each students computers (12 students, 3 different OS, quite a large spread on the CPU speed) we found out that in average, the getter approach is 0.0043% faster... That is on average about 100 nanoseconds.

I assume that the compiler and the JVM does some magic with replacement so that the actual code that are run are more or less the same.

I make that assumption based on a profiling we did. The program spends virtually no time in the getMyObject()-method. I assume that the lookup of the reference takes more or less the same time.

From a readers perspective, I tend to like the second approach better, since that shows me directly what type I am working with. With the first approach, I need to look what the method call is returning. But that is just a personal preference.
Rohan Dhapodkar
Greenhorn

Joined: Jun 27, 2011
Posts: 23
sebastian aguirre wrote:but the variable is something that is created and remains more time occupying memory so I'm not sure.


MyObject will be local variable inside your method and memory will be allocated on stack to hold object reference which will be deallocated once thread is out of method. This assignment will not allocate entirely new memory on heap for object.

[ UD: Edited to fix the incorrect quote tag that attributed text to someone other than the actual author. Please be more careful when quoting people. ]
Daniel Doboseru
Ranch Hand

Joined: Sep 26, 2011
Posts: 57
At least in theory, 2nd approach is better. In faculty I learned that in huge loops (100000 for example) it would be better, to keep procedures calls out as much as possible, and instead use a variable containing the result. Then, when I studied assembly language I learned why: a procedure call, takes longer to complete, than just reading a value from a memory location.

The former also involves more steps to complete than the latter:
1. breaks the current execution flow
2. store the current instruction address
3. call the procedure and execute the instructions within
4. write back the result in memory
5. restore the next instruction address so the execution flow can continue
6. read the result returned from function (or generically called procedure)
...this being executed in every iteration
VS
2nd approach in which only the reading step is performed every time, the rest of them, just one time.

But, as Ove said, the difference is almost insignificant, due to current CPU frequencies, bigger caches, faster memories etc. Also the 2nd version is more elegant, as you can actually see the object.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

which of these two possibilities do you recommend performance wise and why?

Approach 2, but not for any reasons of performance (although I suspect it may be nanoseconds faster).

Simply put, it is clearer; and I'm a great advocate of writing dumb code.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which is more perfomant?
 
Similar Threads
objects and references
Return values of methods
Getting Session Attributes without Scriptlets
Using static
How to access the gettr/setter method of embeded object inside the form bean.