Visual C ++函数突然变慢170毫秒(长4倍) [英] Visual C++ function suddenly 170 ms slower (4x longer)

查看:162
本文介绍了Visual C ++函数突然变慢170毫秒(长4倍)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几个月中,我一直在致力于Visual C ++项目,以从相机拍摄图像并进行处理.到今天为止,大约需要65毫秒来更新数据,但是现在突然增加了.发生的是:我启动程序,并在前30个左右的迭代中按预期方式执行,然后突然将循环时间从65毫秒增加到250毫秒.

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.

奇怪的是,在对每个函数进行计时之后,我发现导致速度下降的那部分代码是相当基本的,并且在一个多月内没有被修改.进入其中的数据在每次迭代中均保持不变且相同,但是最初少于1 ms的执行时间突然增加到170 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).

基本上,我会一遍又一遍地调用相同的函数,因为它最初执行的30次调用应按其应有的性能执行,此后它的运行速度没有明显的原因.值得注意的是执行时间突然改变,而不是逐渐增加.

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.

可能是什么原因造成的?该代码正在泄漏一些内存(〜50 kb/s),但不足以保证突然出现4倍的速度降低.如果有人有任何想法,我很想听听他们!

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!

哇,那太快了!这是减慢速度的代码(减去一些数学运算).我知道这是一个函数,如果您增加行数,计算时间将迅速增加.这里的关键是,使用相同的数据,经过30次迭代后,速度会降低.

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);
            }

        }

    }

}

推荐答案

是否有可能在泄漏了一定数量的内存之后,您的计算机必须开始进/出页面调度?这肯定会减慢甚至简单的功能.

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.

正如有问题的注释所建议的那样,泄漏一定数量的内存也可能会开始将事情从CPU缓存中剔除,这也会使事情变慢.修复内存泄漏,或在此处发布代码供我们查看,都是一个好主意.

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?

这篇关于Visual C ++函数突然变慢170毫秒(长4倍)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆