| Author |
Saving the performance, is this code fine?
|
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 839
|
|
I have a loop of 1-100 line items.
Inside this loop, i do need to calculate the value of boolean flag. then use this flag further for calculations.
Question:
Now i have to call again the same loop second time, for recalculations. boolean flag value's retrieved in 1st call of loop
can be used again by saving it in Map or the same method can be called to know the value of boolean flag.
Is it advised to store the previous results in map, then use it again for 2nd loop call.
Example:
My approach is to save the result for flag in Map for second time calling calculateDuty()
I think my explanation is little vague to understand.
|
SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26496
|
|
Amandeep,
It depends how complex the logic is and if it has dependencies. There isn't really enough information here to have an opinion.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Can the return value of getFlag() change during the loop? If not you could cache the value just before the loop, and use that cached value.
Also, why are you calling calculateDuty() twice? Can it produce different results after calling it again if nothing else has changed?
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 839
|
|
Rob Prime wrote:Can the return value of getFlag() change during the loop? If not you could cache the value just before the loop, and use that cached value.
Also, why are you calling calculateDuty() twice? Can it produce different results after calling it again if nothing else has changed?
Yes the return value of getFlag() can change in 1 loop. Suppose in 1 loop, i loop for 20 line items. So for each line item has its own value.
But when i am calling the loop second time, then i need to use the same flag again for the same 20 line items.
I am calling calculateDuty second time, because some values get change which requires to call it again. But the flag values will definitely remain same.
So question, Can i cache the result in Map, for second time calling.
|
 |
 |
|
|
subject: Saving the performance, is this code fine?
|
|
|