对于两个转置运算,两种的性能明显不一样,是为什么呢?
我电脑的输出是:
0.0473308
0.0265206
#include <iostream>
#include <chrono>
int main(void)
{
int I = 100;
int J = 200;
int K = 300;
int idealI = 105;
// i j k
int* arr = new int[I * J * K];
for (int i = 0; i < I; i++) {
for (int j = 0; j < J; j++) {
for (int k = 0; k < K; k++) {
arr[i * (K * J) + j * K + k] = k;
}
}
}
// k j i
float* transArr = new float[idealI * J * K];
auto startTime = std::chrono::steady_clock::now();
for (int i = 0; i < I; i++) {
for (int j = 0; j < J; j++) {
for (int k = 0; k < K; k++) {
transArr[k * (J * I) + j * I + i] = arr[i * (K * J) + j * K + k] * 0.1f;
}
}
}
auto endTime = std::chrono::steady_clock::now();
std::chrono::duration<double> diffTime = endTime - startTime;
std::cout << diffTime.count() << std::endl;
startTime = std::chrono::steady_clock::now();
for (int i = 0; i < I; i++) {
for (int j = 0; j < J; j++) {
for (int k = 0; k < K; k++) {
transArr[k * (J * idealI) + j * idealI + i] = arr[i * (K * J) + j * K + k] * 0.1f;
}
}
}
endTime = std::chrono::steady_clock::now();
diffTime = endTime - startTime;
std::cout << diffTime.count() << std::endl;
delete[] arr;
delete[] transArr;
return 0;
}
|