Eigen中数据内存访问耗时记录

    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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容