MatrixXi arr = MatrixXi::Random(450, 800);
int h = arr.rows(); int w = arr.cols();
time_t t0 = clock();
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
if (arr(y,x) > 10) arr(y,x)=1;
}
}
printf("time: %.2f ms\n", (float)(clock() - t0)/1000); // 37.8ms
int* ptr = arr.data();
t0 = clock();
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
if (*ptr > 10) *ptr = 1;
ptr++;
}
}
printf("time: %.2f ms\n", (float)(clock() - t0)/1000); // 0.9ms
t0 = clock();
ptr = arr.data();
int* ptr_t;
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
ptr_t = ptr + x*h + y;
// cout << *ptr_t;
}
}
printf("time: %.2f ms\n", (float)(clock() - t0)/1000); // 0.9ms
t0 = clock();
Map<Matrix<int, Dynamic, Dynamic, RowMajor>> arrRow(arr.data(), arr.rows(), arr.cols());
ptr = arrRow.data();
int s = arr.size();
for (int y=0; y<s; y++) {
if (*ptr > 10) *ptr = 1;
ptr++;
}
printf("time: %.2f ms\n", (float)(clock() - t0)/1000); // 0.9ms
网友评论