Time difference in solving arrays in Java

So, I have a random question when coding an image processing function, which is related to time complexity. Below is my original code snippet:

long start = System.currentTimeMillis(); for (int i = 0; i < newWidth; i++) { for (int j = 0; j < newHeight; j++) { double x = i * scaleX; double y = j * scaleY; double xdiff = x - (int) x; double ydiff = y - (int) y; int xf = (int) Math.floor(x); int xc = (int) Math.ceil(x); int yf = (int) Math.floor(y); int yc = (int) Math.ceil(y); double out = inputArray[xf][yf] * (1 - xdiff) * (1 - ydiff) + inputArray[xc][yf] * xdiff * (1 - ydiff) + inputArray[xf][yc] * (1 - xdiff) * ydiff + inputArray[xc][yc] * xdiff * ydiff; outputArray[i][j] = (int) out; } } long elapsed = System.currentTimeMillis() - start; System.out.println("Time used: " + elapsed); 

And after exiting with this code, I was wondering if it would be faster not to create 4 temporary variables for the floor and ceiling values, but instead to calculate them directly in the indexing of the array. So I changed it as follows:

  long start = System.currentTimeMillis(); for (int i = 0; i < newWidth; i++) { for (int j = 0; j < newHeight; j++) { double x = i * scaleX; double y = j * scaleY; double xdiff = x - (int) x; double ydiff = y - (int) y; double out = inputArray[(int) Math.floor(x)][(int) Math.floor(y)] * (1 - xdiff) * (1 - ydiff) + inputArray[(int) Math.ceil(x)][(int) Math.floor(y)] * xdiff * (1 - ydiff) + inputArray[(int) Math.floor(x)][(int) Math.ceil(y)] * (1 - xdiff) * ydiff + inputArray[(int) Math.ceil(x)][(int) Math.ceil(y)] * xdiff * ydiff; outputArray[i][j] = (int) out; } } long elapsed = System.currentTimeMillis() - start; System.out.println("Time used: " + elapsed); 

I expected that later it would be faster (because you do not need to write to a temporary variable and then access it), but it turned out that the later version is at least 2.5 times slower than the previous code. The test case used is a 3x magnification of 1024x768 img.

old code: used time: 812 later code: used time: 2140

So what is the reason for the time difference?

+6
source share
7 answers

Q: How many times do you call "gender" and "replenishment" in the first case? The second case ?;)

Q: I am wondering if this will work faster than the first case:

  long start = System.currentTimeMillis(); for (int i = 0; i < newWidth; i++) { double x = i * scaleX; double xdiff = x - (int) x; int xf = (int) Math.floor(x); int xc = (int) Math.ceil(x); for (int j = 0; j < newHeight; j++) { double y = j * scaleY; double ydiff = y - (int) y; int yf = (int) Math.floor(y); int yc = (int) Math.ceil(y); double out = inputArray[xf][yf] * (1 - xdiff) * (1 - ydiff) + inputArray[xc][yf] * xdiff * (1 - ydiff) + inputArray[xf][yc] * (1 - xdiff) * ydiff + inputArray[xc][yc] * xdiff * ydiff; outputArray[i][j] = (int) out; } } long elapsed = System.currentTimeMillis() - start; System.out.println("Time used: " + elapsed); 
+3
source

You have 8 calculations of Math.floor () / Math.ceil () in later code instead of 4. This is a problem.

The calculation is much slower than allocating space for a local variable.

+6
source

floor and ceil have performance.

The second case causes them twice more often.

+2
source

In the second, you repeat the calculations. By capturing this in a variable in the first example, you will avoid repeating the calculations.

+2
source

Later, it will probably be slower because it does more work. Setting a time variable is trivial, but calculating floors and ceilings can be unexpectedly expensive.

BTW: When x is positive, (int) Math.floor(x) matches (int) x , so you don't need to calculate it twice.

In addition, you can assume that xc = xf + 1 in this situation and similarly for y

+2
source

Small operations of saving and searching for a local variable are pale compared to Math.floor and Math.ceil. In addition, you involve int more often in the second fragment.

+2
source

You can simply convert double to int instead of using Math.floor (). It will be faster.

instead:

 inputArray[(int) Math.floor(x)] 

only

 inputArray[(int) x] 
+1
source

Source: https://habr.com/ru/post/898625/


All Articles