Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My code has a 4D matrix in it for some math problem solving

int**** Sads = new int***[inputImage->HeightLines];
for (size_t i = 0; i < inputImage->HeightLines; i++)
{
    Sads[i] = new int**[inputImage->WidthColumns];
    for (size_t j = 0; j < inputImage->WidthColumns; j++)
    {
        Sads[i][j] = new int*[W_SIZE];
        for (size_t k = 0; k < W_SIZE; k++)
         {
              Sads[i][j][k] = new int[W_SIZE];
         }
    }
 }

//do something with Sads...

for (int i = 0; i < inputImage->HeightLines; i++)
        {
            int*** tempI = Sads[i];
            for (int j = 0; j < inputImage->WidthColumns; j++)
            {
                int** tempJ = tempI[j];
                for (int k = 0; k < W_SIZE; k++)
                {
                    delete[] tempJ[k];
                }
                delete[] Sads[i][j];
            }
            delete[] Sads[i];
        }
        delete[] Sads;

The sizes are very large WidthColumns = 2018, HeightLines = 1332, W_SIZE =7, the memory allocation is very fast but the memory deallocation (delete) is very slow.
Is there a way to optimize it?
I tired openMP but it throws unrelated errors of missing DLL which are there... if I removed the #pragma omp parallel for everything works fine. but slow...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
229 views
Welcome To Ask or Share your Answers For Others

1 Answer

Using a pointer to a pointer to... is a bad idea because it will fragment your data a lot.

I would create a class ta manage the indices transform and use 1D array, it's a bit more complicated but it will be faster.

Anyway, a trick: nothing prevent you to build your int**** with pointers to a zone in memory that isn't sparse (1D array you preallocated) and then use it as a 4D array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...