# 为什么多个计算的运行速度快于数组访问？

``````    for (int r = 0; r < rowMap.length; ++r) {
for (int c = 0; c < columnMap.length; ++c) {
target.setRGB(
c,
r,
source.getRGB(
columnMap[c],
rowMap[r]
)
);
}
}
``````

``````private static double calculateRatio(int sourceValue, int targetValue) {
return (double)sourceValue / (double)targetValue;
}
``````

``````private static int mapByRatio(int targetValue, double sourceToTargetRatio) {
return (int)Math.floor((double)targetValue * sourceToTargetRatio);
}
``````

``````    for (int r = 0; r < targetHeight; ++r) {
for (int c = 0; c < targetWidth; ++c) {
target.setRGB(
c,
r,
source.getRGB(
mapByRatio(c, widthRatio),
mapByRatio(r, heightRatio)
)
);
}
}
``````

``````public static void setMaps(int sourceHeight, int sourceWidth, int targetHeight, int targetWidth) {
columnMap = new int[targetWidth];
rowMap = new int[targetHeight];
double heightRatio = calculateRatio(sourceHeight, targetHeight);
double widthRatio = calculateRatio(sourceWidth, targetWidth);
for (int h = 0; h < targetHeight; ++h) rowMap[h] = mapByRatio(h, heightRatio);
for (int w = 0; w < targetWidth; ++w) columnMap[w] = mapByRatio(w, widthRatio);
}
``````