std::vector reserve() and push_back() is faster than resize() and array index, why?

后端 未结 4 865
余生分开走
余生分开走 2020-12-01 00:59

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

void ConvertToFloat( const std::vector< short >& audioBlock, 
                     std::vec         


        
4条回答
  •  离开以前
    2020-12-01 01:13

    out.resize( audioBlock.size() );
    

    Since out's size (= 0) is lesser than audioBlock.size() , additional elements are created and appended to the end of the out. This creates the new elements by calling their default constructor.

    Reserve only allocates the memory.

提交回复
热议问题