2023-10-11

multidimensional vectors of maps progressively slower after multiple initializations

If I allocate a multidimensional vectors of maps multiple times it get slower and slower. If I try a multidimensional vector of <pairs> then that is ok with each iteration getting the same performance? Why are maps or multimaps different?

void
myReserve(vector<vector<vector<vector<vector<map<int, int>>>>>> myInts, int a, int b, int c, int d, int e)
{
    clock_t begin = clock();
    myInts.reserve(a);
    myInts.resize(a);
    for (int aa = 1; aa < a; aa++)
    {
        myInts[aa].reserve(b);
        myInts[aa].resize(b);
        for (int bb = 1; bb < b; bb++)
        {
            myInts[aa][bb].reserve(c);
            myInts[aa][bb].resize(c);
            for (int cc = 1; cc < c; cc++)
            {
                myInts[aa][bb][cc].reserve(d);
                myInts[aa][bb][cc].resize(d);
                for (int dd = 1; dd < d; dd++)
                {
                    myInts[aa][bb][cc][dd].reserve(e);
                    myInts[aa][bb][cc][dd].resize(e);
                }
            }
        }
    }
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    printf("  ->  elapse time for building is %.4f \n", elapsed_secs);
}
int a = 100, b = 100, c = 30, d = 40, e = 20;
vector<vector<vector<vector<vector<map<int, int>>>>>> myInts;

for (int loop = 1; loop <= 10; loop++)
{
    printf("->  starting loop #%d \n", loop);
    myReserve(myInts, a, b, c, d, e);
}

starting loop #1 elapse time for building is 7.8750 starting loop #2 elapse time for building is 22.7520 starting loop #3 elapse time for building is 20.7190 starting loop #4 elapse time for building is 23.2740 starting loop #5 elapse time for building is 25.5170 starting loop #6 elapse time for building is 27.8460 starting loop #7 elapse time for building is 32.5260 starting loop #8 elapse time for building is 37.5980 starting loop #9 elapse time for building is 44.4400 starting loop #10 elapse time for building is 47.0500



No comments:

Post a Comment