使用std :: shuffle改组向量使用for-循环减慢乘法

问题描述

我正在逐行将2D std :: vector v 与一维std :: vector weights_v 相乘。如果我在乘法之前对 v 的行(使用 std :: shuffle )进行混洗,则乘法循环将花费更多时间来完成。这使我想知道改组的效果。改组实际上会改变内存中数据的顺序,还是仅创建一个将改组后的索引链接到数据位置的映射,而数据位置保持不变?

我使用以下命令来编译程序:

clang++ -Wall -Wextra -Ofast -march=native temp3.cc -o t3 -std=c++17

完成for循环而不进行混洗所需的时间是:

cpp:Pro$ ./t3
loop time is 163.097 ms

洗牌完成后,时间为:

cpp:Pro$ ./t3
loop time is 250.213 ms

此行为的背后原因可能是什么?

编辑:我检查了随机播放前后的随机元素的指针地址,它们的确不同。

#include <iostream>
#include <algorithm>
#include <random>
#include <chrono>
#include <ctime>

int main() {
    std::vector<std::vector<uint8_t>> v(10000);
    std::vector<double> weights_v(784);
    std::vector<uint8_t> y_v(10000),t_v(10000);

    std::minstd_rand rd(100); //initialising and seeding the rng
    std::uniform_int_distribution dist_int_0_255(0,255); //will generate ints between 0 and 255 
    std::uniform_int_distribution dist_int_0_1(0,1);//will generate ints 0 and 1
    std::uniform_real_distribution dist_real(-1,1);//will generate real numbers between -1 and 1 

    for (unsigned long i = 0; i < v.size(); i++) {
        for (int j = 0; j < 785; j++) {
            v[i].push_back(dist_int_0_255(rd));//populating the 2D vector
        }
    }

    for (unsigned long i = 0; i < weights_v.size(); i++) {
        weights_v[i] = dist_real(rd); //populating the weights vector
    }

    for (unsigned long i = 0; i < y_v.size(); i++) {
        y_v[i] = dist_int_0_1(rd);//filling the predicted and target vectors with random 0s and 1s
        t_v[i] = dist_int_0_1(rd);
    }

    int err,T;
    T = 100;
    double eta = 0.25;
    double sum_wx_v;

    //if I comment out the next two lines,the multiplicative loop (LOOP_1) runs faster
    std::mt19937 g(50);
    std::shuffle(std::begin(v),std::end(v),g);

    auto start2 = std::chrono::steady_clock::now(); //start of LOOP_1
    
    for (int iter = 0; iter < T; iter++) {//LOOP_1
        for(unsigned long j = 0; j < v.size(); j++) {
            sum_wx_v = 0.0;
            for (unsigned long k = 1; k < v[0].size() ; k++) {
                sum_wx_v += weights_v[k - 1] * v[j][k];
            }

            //some code to update y_v[i] based on the value of sum_wx_v which I left out

            err = y_v[j] - t_v[j];
            if (fabs(err) > 0) {
                for (unsigned long k = 1; k < v[0].size(); k++) {
                    weights_v[k - 1] -= eta * err * v[j][k];
                }
            }
        }
    }

    auto end2 = std::chrono::steady_clock::now();
    auto diff2 = end2 - start2;
    std::cout << "loop time is "<<std::chrono::duration <double,std::milli> (diff2).count() << " ms" << std::endl;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...