As part of a program I'm building I need to iterate through all the points in a tridimensional matrix. On each point I need to execute a method that takes int x, int y, int z as parameters.
The tridimensional matrix always has the same width(16) length(16) & height(256).
What is the most performant way of doing the iteration?(i'm specially concerned about CPU, not so concerned about ram usage)
Based on what i know , I think this are the most efficient methods, but I'm open to other suggestions if they are faster.
A. Iterate directly:
public void doSomethingForAllPointsInMatrix(Matrix matrix){
for(int x= 0; x<16; x++){
for(int z = 0; z<16; z++){
for(int y = 0; y<256; y++){
matrix.doSomething(x,y,z);//A method out of my control without any alternatives
}
}
}
}
B. Iterate an array containing the coordinates
private static final int[] zeroToFifteen; //Contains every number from 0 to 15
private static final int[] zeroToTwoHundredFiftyFive; //Contains every number from 0 to 255
public void doSomethingForAllPointsInMatrix(Matrix matrix){
for(int x: zeroToFifteen){
for(int z: zeroToFifteen){
for(int y: zeroToTwoHundredFiftyFive){
matrix.doSomething(x,y,z);//A method out of my control without any alternatives
}
}
}
}
Thanks in advance for any help you can provide!