std::vector Reserve() 和 push_back() 比 resize() 和数组索引快,为什么?

std::vector reserve() and push_back() is faster than resize() and array index, why?(std::vector Reserve() 和 push_back() 比 resize() 和数组索引快,为什么?)
本文介绍了std::vector Reserve() 和 push_back() 比 resize() 和数组索引快,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在对一段代码进行快速性能测试

I was doing a quick performance test on a block of code

void ConvertToFloat( const std::vector< short >& audioBlock, 
                     std::vector< float >& out )
{
    const float rcpShortMax = 1.0f / (float)SHRT_MAX;
    out.resize( audioBlock.size() );
    for( size_t i = 0; i < audioBlock.size(); i++ )
    {
        out[i]  = (float)audioBlock[i] * rcpShortMax;
    }
}

我对处理 65536 个音频样本只需要 1 毫秒多一点的原始非常幼稚的实现的速度感到满意.

I was happy with the speed up over the original very naive implementation it takes just over 1 msec to process 65536 audio samples.

不过为了好玩,我尝试了以下方法

However just for fun I tried the following

void ConvertToFloat( const std::vector< short >& audioBlock, 
                     std::vector< float >& out )
{
    const float rcpShortMax = 1.0f / (float)SHRT_MAX;
    out.reserve( audioBlock.size() );
    for( size_t i = 0; i < audioBlock.size(); i++ )
    {
        out.push_back( (float)audioBlock[i] * rcpShortMax );
    }
}

现在我完全期望这能提供与原始代码完全相同的性能.然而,突然之间,循环现在占用了 900 微秒(即它比其他实现快 100 微秒).

Now I fully expected this to give exactly the same performance as the original code. However suddenly the loop is now taking 900usec (i.e. it's 100usec faster than the other implementation).

谁能解释为什么这会提供更好的性能?resize() 是否初始化新分配的向量,而reserve 只是分配但不构造?这是我唯一能想到的.

Can anyone explain why this would give better performance? Does resize() initialize the newly allocated vector where reserve just allocates but does not construct? This is the only thing I can think of.

PS 这是在单核 2Ghz AMD Turion 64 ML-37 上测试的.

PS this was tested on a single core 2Ghz AMD Turion 64 ML-37.

推荐答案

resize 是否初始化新分配的向量,而reserve 只分配但不构造?

Does resize initialize the newly allocated vector where reserve just allocates but does not construct?

是的.

这篇关于std::vector Reserve() 和 push_back() 比 resize() 和数组索引快,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

OpenGL transforming objects with multiple rotations of Different axis(OpenGL 变换不同轴多次旋转的对象)
GLFW first responder error(GLFW 第一响应者错误)
SOIL not linking correctly(SOIL 连接不正确)
Core profile vs version string? Only getting GLSL 1.3/OGL 3.0 in mesa 10.0.1(核心配置文件与版本字符串?在 mesa 10.0.1 中只获得 GLSL 1.3/OGL 3.0)
What is the range of OpenGL texture ID?(OpenGL 纹理 ID 的范围是多少?)
How taxing are OpenGL glDrawElements() calls compared to basic logic code?(与基本逻辑代码相比,OpenGL glDrawElements() 调用的繁重程度如何?)