在某个阈值后,Std :: vector填充时间从0ms到16ms?

jma*_*erx 1 c++ performance vector

这就是我正在做的事情.我的应用程序在拖动时从用户获取点并实时显示填充多边形.

它基本上在MouseMove上添加了鼠标位置.这一点是一个USERPOINT并且具有更好的句柄,因为最终我会做得更好,这就是为什么我必须将它们转换为向量.

所以基本上是MousePos - > USERPOINT.USERPOINT被添加到a std::vector<USERPOINT>.然后在我的UpdateShape()函数中,我这样做:

DrawingPoints定义如下:

std::vector<std::vector<GLdouble>> DrawingPoints;


Contour[i].DrawingPoints.clear();



 for(unsigned int x = 0; x < Contour[i].UserPoints.size() - 1; ++x)
         SetCubicBezier(
             Contour[i].UserPoints[x],
             Contour[i].UserPoints[x + 1],
             i);
Run Code Online (Sandbox Code Playgroud)

SetCubicBezier()目前看起来像这样:

void OGLSHAPE::SetCubicBezier(USERFPOINT &a,USERFPOINT &b, int &currentcontour )
{
std::vector<GLdouble> temp(2);

    if(a.RightHandle.x == a.UserPoint.x && a.RightHandle.y == a.UserPoint.y 
        && b.LeftHandle.x == b.UserPoint.x && b.LeftHandle.y == b.UserPoint.y )
    {

        temp[0] = (GLdouble)a.UserPoint.x;
        temp[1] = (GLdouble)a.UserPoint.y;

        Contour[currentcontour].DrawingPoints.push_back(temp);

        temp[0] = (GLdouble)b.UserPoint.x;
        temp[1] = (GLdouble)b.UserPoint.y;


        Contour[currentcontour].DrawingPoints.push_back(temp);

    }
    else
    {
         //do cubic bezier calculation
        }
Run Code Online (Sandbox Code Playgroud)

因此,由于立方贝塞尔的原因,我需要将USERPOINTS设置为GlDouble [2](因为GLUTesselator接受一个double的静态数组).

所以我做了一些分析.在~100点,代码:

 for(unsigned int x = 0; x < Contour[i].UserPoints.size() - 1; ++x)
         SetCubicBezier(
             Contour[i].UserPoints[x],
             Contour[i].UserPoints[x + 1],
             i);
Run Code Online (Sandbox Code Playgroud)

执行时间为0 ms.然后大约120,它跳到16毫秒,永远不会回头.我很肯定这是由于std :: vector.我该怎么做才能让它保持0ms.我不介意在生成形状时使用大量内存,然后在形状完成时删除多余的东西,或类似的东西.

GMa*_*ckG 12

0毫秒没时间......没有时间执行任何东西.这应该是您的第一个指标,您可能需要检查计时方法的时间结果.

也就是说,定时器通常没有良好的分辨率.您的16ms之前的结果可能实际上只有1ms - 在0ms时错误地报告了15ms.无论如何,如果我们能告诉你如何将它保持在0毫秒,我们就会变得富有而着名.

相反,找出循环的哪些部分花费最长,并优化它们.不要朝着任意时间的方向努力.我建议找一个好的剖析器来获得准确的结果.然后你不需要猜测什么是慢的(循环中的东西),但实际上可以看到哪个部分很慢.

  • "如果我们能告诉你如何将它保持在0毫秒,我们就会变得富有而且很有名" - 非常喜欢它. (7认同)
  • Windows计时器(由`GetTickCount`使用)的分辨率仅为15-16毫秒,这可以解释结果. (4认同)