Forums Register Login

which is more perfomant?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
The 2nd approach should be faster, but whether that makes a noticeable difference depends on how complex the getMyObject method is.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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....
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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. ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
You've gotta fight it! Don't give in! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1250 times.
Similar Threads
Getting Session Attributes without Scriptlets
Return values of methods
Using static
How to access the gettr/setter method of embeded object inside the form bean.
objects and references
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:25:08.