• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving the performance, is this code fine?  RSS feed

 
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amandeep,
It depends how complex the logic is and if it has dependencies. There isn't really enough information here to have an opinion.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Amandeep Singh
Ranch Hand
Posts: 853
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!