【问题标题】:I don't understand why my c++ code running so slow我不明白为什么我的 c++ 代码运行这么慢
【发布时间】:2016-12-07 06:38:06
【问题描述】:

我正在研究用于边缘检测的 Sobel 掩码,而不使用任何特殊库。我想要得到的输出是一个具有 512x512 矩阵的文本文件,其值在 0 到 1 之间。 我通过放置较小的值(例如 50)而不是“ROW-2”和“COL-2”来检查代码是否正常工作。 但是,如果我把它们放回去,代码需要永远运行。

常数值为:

const int ROW = 512;
const int COL = 512;
const double Gx [3][3] = { {-1.0,0.0,1.0},{-2.0,0.0,2.0},{-1.0,0.0,1.0}};
const double Gy [3][3] = { {1.0,2.0,1.0},{0.0,0.0,0.0},{-1.0,-2.0,-1.0}};

这是主要功能:

int main()

{  
    double NewImage[ROW][COL] = {0};    

    for (int i = 0; i < ROW; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            NewImage[i][j] = 0;
        }
    }

    for (int i = 0; i < ROW-2; i++)
    {
        for (int j = 0; j < COL-2; j++)
        {

            NewImage[i+1][j+1] = SobelConvolution(i,j); 
        }
    }

    ofstream newImage;
    string filename;
    filename = "output image.txt";

    newImage.open (filename.c_str());

    for(int rows = 0; rows < ROW; rows++)
    {
        for(int cols = 0; cols < COL; cols++)
        {
            newImage << NewImage[ROW][COL] <<" ";
        }
        newImage << endl;
    }

    newImage.close();

    return 0;
}

这是SobelConvolution函数:

double SobelConvolution(int row, int col)
{   
    double convX;
    double convY;
    double conv;

    convX = ImageReader(row,col)*Gx[2][2]
            + ImageReader(row,col+1)*Gx[2][1]
            + ImageReader(row,col+2)*Gx[2][0]
            + ImageReader(row+1,col)*Gx[1][2]
            + ImageReader(row+1,col+1)*Gx[1][1]
            + ImageReader(row+1,col+2)*Gx[1][0]
            + ImageReader(row+2,col)*Gx[0][2]
            + ImageReader(row+2,col+1)*Gx[0][1]
            + ImageReader(row+2,col+2)*Gx[0][0];

    convY = ImageReader(row,col)*Gy[2][2]
            + ImageReader(row,col+1)*Gy[2][1]
            + ImageReader(row,col+2)*Gy[2][0]
            + ImageReader(row+1,col)*Gy[1][2]
            + ImageReader(row+1,col+1)*Gy[1][1]
            + ImageReader(row+1,col+2)*Gy[1][0]
            + ImageReader(row+2,col)*Gy[0][2]
            + ImageReader(row+2,col+1)*Gy[0][1]
            + ImageReader(row+2,col+2)*Gy[0][0];

    conv = sqrt((convX*convX) + (convY*convY));


    return conv;
}

这是ImageReader函数:

double ImageReader(int r, int c)
{
    double OrigImage[ROW][COL];

    ifstream defaultImage ("image.txt");

    if (defaultImage.good())
    {
        for (int i = 0; i < ROW; i++)
        {
            for (int j = 0; j < COL; j++)
            {
                defaultImage >> OrigImage[i][j];
            }
        }
    }
    return OrigImage [r][c]; 
}

有什么提示或建议吗?提前致谢!

【问题讨论】:

  • 您是否有意忽略NewImage 的第一个和最后一个值(将它们留给0)?
  • 是的。我认为这种方式不会那么复杂,而不必担心边缘的值。这就是程序导致问题的原因吗?
  • 不 :) 有太多不必要的循环。 ;)
  • 如果您没有在启用优化的情况下进行编译,您应该 - 例如,clang 和 gcc 编译器的 -O2 或 -O3 选项。

标签: c++ performance edge-detection sobel


【解决方案1】:

这里有一些注意事项:

  • ImageReader

    只返回数组的一个值,无需读取整个整个数组每次只需要一个值。在我看来,这个功能是多余的。

  • SobelConvolution

    这个函数不错,但是有一个不必要的变量——conv。

  • main

    我不知道你为什么要将NewImage 的每个值初始化为0,而它们已经是0!你实际上也不需要NewImage

这是我要写的内容(带有大量评论):

double SobelConvolution(int row, int col)
{
    //ImageReader has been removed, it was unnecessary. The code has been moved here
    double oldImage[ROW][COL];
    std::ifstream defaultImage{ "image.txt" };

    //Error handling if file doesn't exist - consider doing something else :)
    if (!defaultImage.is_open())
        return 0;

    //Initialize array
    for (int i = 0; i < ROW; ++i)
        for (int j = 0; j < COL; ++j)
            defaultImage >> oldImage[i][j];

    //You should always declare variables where they are first used, this
    //reduces the possibility of errors
    //We can just access the array directly
    double convX = oldImage[row][col] * Gx[2][2]
        + oldImage[row][col + 1] * Gx[2][1]
        + oldImage[row][col + 2] * Gx[2][0]
        + oldImage[row + 1][col] * Gx[1][2]
        + oldImage[row + 1][col + 1] * Gx[1][1]
        + oldImage[row + 1][col + 2] * Gx[1][0]
        + oldImage[row + 2][col] * Gx[0][2]
        + oldImage[row + 2][col + 1] * Gx[0][1]
        + oldImage[row + 2][col + 2] * Gx[0][0];

    double convY = oldImage[row][col] * Gy[2][2]
        + oldImage[row][col + 1] * Gy[2][1]
        + oldImage[row][col + 2] * Gy[2][0]
        + oldImage[row + 1][col] * Gy[1][2]
        + oldImage[row + 1][col + 1] * Gy[1][1]
        + oldImage[row + 1][col + 2] * Gy[1][0]
        + oldImage[row + 2][col] * Gy[0][2]
        + oldImage[row + 2][col + 1] *Gy[0][1]
        + oldImage[row + 2][col + 2]*Gy[0][0];

    //No need to create a separate variable just to return it
    return sqrt((convX*convX) + (convY*convY));
}


int main()
{
    //= {} Initializes every element to 0, you don't need to do it :) Just so you know :)
    //Note that it crashes here, because my stack size was too small,
    //maybe consider using a dynamic array (512 * 512 is pretty big) :)
    //double NewImage[ROW][COL] = {};
    //The array is not really needed, see below

    std::string filename = "oimage.txt";
    std::ofstream newImage{ filename };

    //No need to create another array just to output it again,
    //Just output the calculated values - this doesn't ignore the first/last values
    for (int rows = 0; rows < ROW; rows++)
    {
        for (int cols = 0; cols < COL; cols++)
            newImage << SobelConvolution(rows, cols) << " ";
        newImage << '\n'; //std::endl flushes the stream, while \n does not - it is faster :)
    }

    newImage.close();

    return 0;
}

【讨论】:

  • 我花了一点时间才看完你的评论。谢谢!它真的帮了我很多。我不完全确定以这种方式声明数组,所以它让我放置了更多变量来澄清,这使得它过于多余。
  • @RyanKim 欢迎您,如果您还有任何问题,请随时提问 :)
【解决方案2】:

你真的是指打开单个图像文件 18 次并读取每一行和每一列的所有数据,只是为了返回单行和列 18 次?为什么不读取一次图像文件并将图像数据数组传递给函数?

【讨论】:

  • 哦,我只是认为它是一个价值,却忘记了每次这样做时我实际上都是在打开整个图像文件。谢谢!
  • @RyanKim 你每次都在遍历整个数组:)
【解决方案3】:

你所做的不仅仅是效率低下,而且——对不起——完全疯了。

对于图像的每个像素,您调用 SobelConvolution,后者又调用 ImageReader 18 次(其中 6 次没有用,因为相应的系数为零)。但可怕的是,ImageReader 每次都会从文本文件中执行完整的图像读取,其中一个简单的数组查找就足够了。

因此,您总共执行了 4718592 次文件流打开/关闭和 1236950581248 次文件流读取,其中只需 1 次打开/关闭和 262144 次读取就足够了。 (不算单次读取比直接访问数组的成本要高得多。)一次完整的运行可能会持续两个小时或更长时间。

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 1970-01-01
    • 2012-05-25
    • 2023-04-01
    • 1970-01-01
    • 2023-03-28
    • 2019-09-07
    相关资源
    最近更新 更多