且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

时间性能,当置换和铸造双重浮动

更新时间:2023-08-18 12:00:28

好的,让我们通过尽快预先计算一下来解开你的循环:

Ok, let's unravel your loop a little bit by precalculating things ASAP:

int max0 = size_proj[0];
int max1 = size_proj[1];
int max2 = size_proj[2];

for (int k = 0; k < max0; k++)
{
    int kOffset1 = k*max1;
    int kOffset2 = k;

    for (int i = 0; i < max1; i++)
    {
        int iOffset1 = i;
        int iOffset2 = i*max0;

        for (int j = 0; j < max2; j++)
        {
            int jOffset1 = j*max0*max1;
            int jOffset2 = j*max0*max1;


            int idx1 = iOffset1 + jOffset1 + kOffset1;
            int idx2 = iOffset2 + jOffset2 + kOffset2;
            img[idx1] = (float)imgaux[idx2];
        }
    }
}

$ c> jOffset1 / 2 似乎不是***的嵌套循环的最低级别。这总是使得 max0 * max1 idx1 / 2 值跳转。让我们把它移动到***别:

The calculation for jOffset1/2 seems to be suboptimal being on the lowest level of your nested loop. This always makes the idx1/2 value jump for max0*max1 every iteration. So let's move this to the highest level:

int max0 = size_proj[0];
int max1 = size_proj[1];
int max2 = size_proj[2];
for (int j = 0; j < max2; j++)
{
    int jOffset1 = j*max0*max1;
    int jOffset2 = j*max0*max1;

    for (int k = 0; k < max0; k++)
    {
        int kOffset1 = k*max1;
        int kOffset2 = k;

        for (int i = 0; i < max1; i++)
        {
            int iOffset1 = i;
            int iOffset2 = i*max0;

            int idx1 = iOffset1 + jOffset1 + kOffset1;
            int idx2 = iOffset2 + jOffset2 + kOffset2;
            img[idx1] = (float)imgaux[idx2];
        }
    }
}

kOffset1 / 2 iOffset1 / 2 无法优化,但我们仍然有不必要的值和声明。让我们总结一下:

That already looks better. kOffset1/2 and iOffset1/2 can't be optimized anymore, but we still have unecessary values and declarations. Let's sum these up:

for (int j = 0; j < size_proj[2]; j++)
{
    int jOffset = j*size_proj[0]*size_proj[1];
    for (int k = 0; k < size_proj[0]; k++)
    {
        int kOffset1 = k*size_proj[1];
        for (int i = 0; i < size_proj[1]; i++)
        {
            int iOffset2 = i*size_proj[0];
            img[i + jOffset + kOffset1] = (float)imgaux[iOffset2 + jOffset + k];
        }
    }
}






我用你的循环和我的(使用MSVC14的同一个系统)你更新了MVCE:


I tried your updated MVCE with your loop and with mine (same system using MSVC14):

你的:


时间置换和投射输入4.180000

Time permuting and casting the input 4.180000

Mine:


时间置换和投射输入0.704000

Time permuting and casting the input 0.704000

希望我' - )

正如@BarryTheHatchet指出的,因为它在注释部分很容易监督:对 size_proj 使用3 int 数组的数组,***使用三个 const int 值。

As @BarryTheHatchet pointed out and as it is easily overseen in the comment section: Instead of using an array of 3 int values for size_proj you better use three const int values.

不使用数组会从代码中删除复杂性(使用描述性名称)
使用 const 将阻止您在复杂计算中意外更改值,并允许编译器进行更好的优化。

Not using an array will remove complexity from your code (using descriptive names of course) The use of const will prevent you from accidentially changing values in complex calculation and may allow the compiler for better optimization.

正如@paddy指出:您可以通过预计算步长来计算嵌套循环的不同级别的乘法。

As @paddy pointed out: You may replace the multiplications at the different levels of your nested loop with calculations by precalculating the step sizes.

但是乘法版本和步骤版本没有任何真正的变化....

I had tried this but there wasn't any real change in the multiplication version and step version....

const int jStep     = size_proj[0] * size_proj[1];
const int jStepMax  = size_proj[0] * size_proj[1] * size_proj[2];
const int kStep1 = size_proj[1];
const int kStep1Max = size_proj[0] * size_proj[1];
const int kStep2 = 1;
const int kStep2Max = size_proj[0];
const int iStep1 = 1;
const int iStep1Max = size_proj[1];
const int iStep2 = size_proj[0];
const int iStep2Max = size_proj[0] * size_proj[1];

for (int jOffset = 0; jOffset < jStepMax; jOffset += jStep)
{
    for (int kOffset1 = 0, kOffset2=0; kOffset1 < kStep1Max && kOffset2 < kStep2Max; kOffset1+=kStep1, kOffset2+=kStep2)
    {
        for (int iOffset1 = 0, iOffset2 = 0; iOffset1 < iStep1Max && iOffset2 < iStep2Max; iOffset1 += iStep1, iOffset2 += iStep2)
        {
            img[iOffset1 + jOffset + kOffset1] = (float)imgaux[iOffset2 + jOffset + kOffset2];
        }
    }
}