#how to stop duplicated calculations in methods

5 messages · Page 1 of 1 (latest)

chrome niche
#

hi, i have a method but it's heavy in performance, i am trying to optimize it, so this is the method body but i want to optimize it more by adding if else statements that runs first to check if x, y, z has the same data, so if x, y, z has the same data this code shouldn't be run

//if x, y, z has the same value of it, let's say (x = 10, y = 10, z = 10) and the returned value of blockToSectionCoord is 10, this calculations should be stopped //so the method returns the TestClass(x, y, z) a lot more faster //disable assigning values if the returned data is the same int x = blockToSectionCoord(pos.getX()); int y = blockToSectionCoord(pos.getY()); int z = blockToSectionCoord(pos.getZ());

the reason of stopping this code is that
if x = 10, y = 10, z = 10
and the returned value of blockToSectionCoord(pos.getX)) and blockToSectionCoord(pos.getY()); and blockToSectionCoord(pos.getZ());` is the same = 10
duplicating this calculations is necessary

Also does adding if else statement to avoid duplicating the calculations will be faster than this?

thorn apexBOT
#

This post has been reserved for your question.

Hey @chrome niche! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

split plaza
#

I know a simple way if you are willing to work with hashmaps

#

With hashmaps there is a function called computeIfAbsent that only does the calculation if the data is not in already in the map