Visual C++函数突然慢 170 毫秒(长 4 倍)

发布于 2024-09-05 23:02:17 字数 1247 浏览 11 评论 0原文

在过去的几个月里,我一直致力于一个 Visual C++ 项目,从相机中获取图像并进行处理。到目前为止,更新数据大约需要 65 毫秒,但现在突然显着增加。发生的情况是:我启动程序,在前 30 次左右的迭代中,它按预期执行,然后循环时间突然从 65 毫秒增加到 250 毫秒。

奇怪的是,在对每个函数进行计时后,我发现导致速度变慢的代码部分相当基本,并且一个多月没有修改。每次迭代中输入的数据都保持不变且相同,但最初小于 1 毫秒的执行时间突然增加到 170 毫秒,而代码的其余部分仍然按预期执行(时间方面)。

基本上,我一遍又一遍地调用同一个函数,前 30 次调用它会按预期执行,之后它会无缘无故地变慢。还值得注意的是,这是执行时间的突然变化,而不是逐渐增加。

可能是什么原因造成的?该代码泄漏了一些内存(~50 kb/s),但不足以保证突然减慢 4 倍。如果有人有任何想法,我很乐意听到他们!

编辑:哇,速度真快!这是减慢速度的代码(减去一些数学)。我知道这是一个如果增加行数则计算时间会迅速增加的函数。这里的关键是,使用相同的数据,30 次迭代后速度会变慢。

void CameraManager::IntersectLines()
{

    // Two custom classes
    TMaths maths;
    TLine line1, line2;

    while(lines.size()>0)
    {

        // Save the current line
        line1 = lines[0];

        // Then remove it from the list
        lines.erase(lines.begin());

        CvMat* aPoint;
        for (int i = 0; i<lines.size(); i++)
        {

            line2 = lines[i];

            aPoint = cvCreateMat(1, 4, CV_32FC1);

            // Calculate the point of intersection
            maths.Intersect(line1.xyz, line2.xyz, line1.uvw, line2.uvw, aPoint);

            // Add the point to the list
            points.push_back(aPoint);
            }

        }

    }

}

For the past few months I've been working on a Visual C++ project to take images from cameras and process them. Up until today this has taken about 65 ms to update the data but now it has suddenly increased significantly. What happens is: I launch my program and for the first 30 or so iterations it performs as expected, then suddenly the loop time increases from 65 ms to 250 ms.

The odd thing is, after timing each function I found out that the part of the code which is causing the slowdown is fairly basic and has not been modified in over a month. The data which goes into it is unchanged and identical every iteration but the execution time which is initially less than 1 ms suddenly increases to 170 ms while the rest of the code is still performing as expected (time-wise).

Basically, I am calling the same function over and over, for the first 30 calls it performs as it should, after that it slows down for no apparent reason. It might also be worth noting that it is a sudden change in execution time, not a gradual increase.

What could be causing this? The code is leaking some memory (~50 kb/s) but not nearly enough to warrant a sudden 4x slowdown. If anyone has any ideas I'd love to hear them!

Edit: Wow, that was fast! Here's the code (minus some maths) which slows down. I know this is a function where the computational time will increase rapidly if you increase the number of lines. The key here is that with the same data this slows down after 30 iterations.

void CameraManager::IntersectLines()
{

    // Two custom classes
    TMaths maths;
    TLine line1, line2;

    while(lines.size()>0)
    {

        // Save the current line
        line1 = lines[0];

        // Then remove it from the list
        lines.erase(lines.begin());

        CvMat* aPoint;
        for (int i = 0; i<lines.size(); i++)
        {

            line2 = lines[i];

            aPoint = cvCreateMat(1, 4, CV_32FC1);

            // Calculate the point of intersection
            maths.Intersect(line1.xyz, line2.xyz, line1.uvw, line2.uvw, aPoint);

            // Add the point to the list
            points.push_back(aPoint);
            }

        }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

合久必婚 2024-09-12 23:02:17

是否有可能在泄漏一定量的内存后,您的计算机必须开始将内容调入/调出?即使是简单的功能,这肯定会减慢速度。

如果不知道该函数的作用,就很难确切地说出是什么导致了问题。

编辑:正如问题评论中所建议的那样,泄漏一定量的内存也可能会开始将CPU缓存中的内容删除,这也会减慢速度。要么修复内存泄漏,要么将代码发布在这里供我们查看,都是一个好主意。

编辑 2:您在该循环中调用了几个函数。除了简单的算术计算之外,他们还会做其他事情吗?

Is it possible that after leaking a certain amount of memory, your computer has to start paging stuff in/out? That would definitely slow down even simple functions.

Without knowing what the function does, it's hard to say exactly what could be causing the problem.

Edit: As suggested in question comments, leaking a certain amount of memory could also start knocking things out of the CPU cache, which will also slow things down. Either fixing the memory leak, or posting the code here for us to look at, would be a good idea.

Edit 2: You call a couple of functions in that loop. Do they do anything other than simple arithmetic calculations?

过度放纵 2024-09-12 23:02:17

如果它泄漏了足够的内存来填满一个页面(50KB/s可能就足够了),那么Windows将不得不切换页面来处理数据。当这种情况发生时,程序的效率就会变得很低。

If it leaks enough memory to fill up a page (50KB/s may be enough), then Windows will have to switch pages to handle the data. When this happens, the program becomes much more inefficient.

过度放纵 2024-09-12 23:02:17

显然有些事情已经改变了。尝试将代码恢复到速度减慢之前的状态。如果速度又快了,请关注代码更改。如果速度很慢,请在代码之外查找问题。比如数据库、操作系统等。

Obviously something has changed. Try reverting your code to what it was before the slow down. If it then is fast again, focus on the code changes. If it is slow, then look for the problem outside of your code. Things like the database, the OS, etc.

梦一生花开无言 2024-09-12 23:02:17

如果您遇到内存泄漏,那么您的代码中肯定出现了问题。首先解决这个问题,你很可能会发现你的问题,或者至少消除一个相当明显的嫌疑。

正如其他人提到的,如果您提供一些代码,尝试帮助您会更容易...

[编辑] 我猜您的 cvCreateMat 函数分配了一些记忆?它会在任何地方被释放吗?

If you've got a memory leak, there's definitely something bad happening in your code. Fix that first and chances are you'll discover your problem or at least eliminate one fairly obvious suspect.

As others have mentioned, it would be much easier to try and help you if you provided some code...

[Edit] I'd guess that your cvCreateMat function allocates some memory? Does that ever get freed anywhere?

凉世弥音 2024-09-12 23:02:17

需要删除容器中的每一行吗?

    // Then remove it from the list
    lines.erase(lines.begin());

根据容器的不同,擦除元素(一次一个)可能会很慢,尤其是对于大型容器。我建议在计算期间使用起始索引。在调用函数之前提前索引。索引和迭代器的更新速度比擦除元素更快。计算完成后,您可以删除或擦除整个容器(如果容器有,请参阅 clear 方法)。 clear 方法可能比一次擦除一个元素更快。

Do you need to erase each line in the container?

    // Then remove it from the list
    lines.erase(lines.begin());

Depending on the container, erasing elements (one at a time) may be slow especially for large containers. I suggest using a starting index during the computation. Advance the index before calling the function. Indexes and iterators are quicker to update than erasing an element. After the computation, you can remove or erase the entire container (see the clear method if the container has one). The clear method may be faster than erasing one element at a time.

雨巷深深 2024-09-12 23:02:17

分析代码,这样您就不需要猜测答案。

当然,无论如何,这可能会改变代码的性能,但这是准确了解代码发生情况的最直接方法......

(根据此问题的当前最佳答案:在 Visual Studio 2008 PRO 中进行分析 您需要 VS 2008 的“团队”版本才能使用内置分析器,否则您需要使用外部分析器)

Profile the code, then you won't need to guess at answers.

Of course, this may change the performance of the code anyway, but it's the most direct method of seeing exactly what is going on with your code...

(According to the current top answer to this question : Profiling in Visual Studio 2008 PRO you need the "Team" edition of VS 2008 to use the built-in profiler, otherwise you'll need to use an external profiler)

×纯※雪 2024-09-12 23:02:17

如果您想要任何有用的答案,您需要使您的问题更加具体。

You need to make your question a lot more specific if you want any useful answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文