为什么std :: vector比数组指针慢得多

时间:2018-03-31 07:34:03

标签: c++ opengl

我正在创建一个Opengl字体批量绘图。

我想知道为什么我的std :: vector比使用数组指针慢得多。

我尝试过添加矢量储备和我能想到的一切。

使用载体时,我的FPS下降了一半。

向量似乎更容易管理并且在代码中看起来更好,但我真的坚持这个。

不确定我错过了哪些简单的东西?

或者使用数组指针会更好吗?

#ifdef USE_VECTORS
    std::vector<Vertex> m_vertices;
#else
    Vertex *m_pVertex;
    Vertex m_vertices[MAX_VERTICES];
#endif

绘制字符函数

    void GLFont::drawChar(char c, int x, int y)
    {
        //  1------4
        //  |      |            1 = (x, y)
        //  |      |            2 = (x, y + charHeight)
        //  |      |            3 = (x + charWidth, y + charHeight)
        //  |      |            4 = (x + charWidth, y)
        //  |      |
        //  |      |
        //  2------3
        //

        const Glyph &glyph = getChar(c);
        int charWidth = glyph.width;
        int charHeight = m_charHeight;

    #ifdef USE_VECTORS
        Vertex vert[] = {
            x, y,
            glyph.upperLeft[0], glyph.upperLeft[1],
            m_color[0], m_color[1], m_color[2], m_color[3],

            x, y + charHeight,
            glyph.lowerLeft[0], glyph.lowerLeft[1],
            m_color[0], m_color[1], m_color[2], m_color[3],

            x + charWidth, y + charHeight,
            glyph.lowerRight[0], glyph.lowerRight[1],
            m_color[0], m_color[1], m_color[2], m_color[3],

            x + charWidth, y,
            glyph.upperRight[0], glyph.upperRight[1],
            m_color[0], m_color[1], m_color[2], m_color[3]
        };
        //unsigned dataArraySize = sizeof(vert) / sizeof(Vertex);

        m_vertices.insert(m_vertices.end(), &vert[0], &vert[4]);

        ++m_numCharsToDraw;
    #else
         //1
        m_pVertex->x = x;
        m_pVertex->y = y;
        m_pVertex->s = glyph.upperLeft[0];
        m_pVertex->t = glyph.upperLeft[1];
        m_pVertex->r = m_color[0];
        m_pVertex->g = m_color[1];
        m_pVertex->b = m_color[2];
        m_pVertex->a = m_color[3];
        ++m_pVertex;

        // 2
        m_pVertex->x = x;
        m_pVertex->y = y + charHeight;
        m_pVertex->s = glyph.lowerLeft[0];
        m_pVertex->t = glyph.lowerLeft[1];
        m_pVertex->r = m_color[0];
        m_pVertex->g = m_color[1];
        m_pVertex->b = m_color[2];
        m_pVertex->a = m_color[3];
        ++m_pVertex;

        // 3
        m_pVertex->x = x + charWidth;
        m_pVertex->y = y + charHeight;
        m_pVertex->s = glyph.lowerRight[0];
        m_pVertex->t = glyph.lowerRight[1];
        m_pVertex->r = m_color[0];
        m_pVertex->g = m_color[1];
        m_pVertex->b = m_color[2];
        m_pVertex->a = m_color[3];
        ++m_pVertex;

        // 4
        m_pVertex->x = x + charWidth;
        m_pVertex->y = y;
        m_pVertex->s = glyph.upperRight[0];
        m_pVertex->t = glyph.upperRight[1];
        m_pVertex->r = m_color[0];
        m_pVertex->g = m_color[1];
        m_pVertex->b = m_color[2];
        m_pVertex->a = m_color[3];
        ++m_pVertex;

        if (++m_numCharsToDraw == MAX_CHARS_PER_BATCH)
        {
            drawTextEnd();
            drawBatchOfChars();
            drawTextBegin();
        }
    #endif
    }

void GLFont::drawBatchOfChars()
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

#ifdef USE_VECTORS
    glVertexPointer(2, GL_INT, sizeof(Vertex), &m_vertices[0].x);
    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &m_vertices[0].s);
    glColorPointer(4, GL_FLOAT, sizeof(Vertex), &m_vertices[0].r);
#else
    glVertexPointer(2, GL_INT, sizeof(Vertex), &m_vertices->x);
    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &m_vertices->s);
    glColorPointer(4, GL_FLOAT, sizeof(Vertex), &m_vertices->r);
#endif

    glDrawArrays(GL_QUADS, 0, m_numCharsToDraw * 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
}

2 个答案:

答案 0 :(得分:5)

您正在将苹果与橙子进行比较:

  • 容器本身包含指针,减少了数据的位置。
  • 容器在更改大小时需要重新分配存储空间。
  • Microsoft的Visual Studio在其标准库容器中启用了其他诊断功能。例如,如果索引超出范围,则通过vec[123]访问会导致每个标准的未定义行为。这允许通过指向数组有效载荷的指针将其实现为简单索引。通过额外的诊断,索引被验证,这只是一个小比较和分支,但在紧密循环中它会产生影响。

那就是说,你证明某事的方法是有缺陷的。您首先必须实现尽可能减少的等效代码(本着MCVE的精神)。将OpenGL后端连接到它上面并不能使事情重现。

答案 1 :(得分:0)

您看到较大性能差异的主要原因是调试模式。

在调试模式下,MSVC默认不进行任何内联,不会在寄存器中存储任何变量,并且总是分配和验证堆栈帧,没有名称。

在调试模式下使用向量时的额外抽象直接转换为CPU的更多工作。