• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which is more perfomant?

 
sebastian aguirre
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 2nd approach should be faster, but whether that makes a noticeable difference depends on how complex the getMyObject method is.
 
sebastian aguirre
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic