计算机技術文摘

如何解决大量字符串的拼接操作的耗时问题

std::vector< std::string > results;
results.resize( m_cluster_num, "" );
char temp[255];
for( int i=0; i< m_data_num; i++ )
{
      label_index = m_labels->at(i);
      if ( label_index < m_cluster_num )
      {
            temp[0]='\0';
            results.at( label_index ) = results.at( label_index ) + temp; 
      }
}
对于上面的这段代码,如果m_data_num为百万级别,那么其执行的时间将耗费数小时,其主要的原因就在于string的+操作符实质上是重载了+=操作符,所以,每次都会生成一个临时对象,还要赋回给原值,在参与拼接的串达到百万级别时,速度可想而知,用
 results.at( label_index )  += temp;
代替后,时间立刻降到百秒以内,效果十分明显! 

posted on 2010-03-21 00:38 计算机技术博客 阅读(302) 评论(0)  编辑 收藏 引用 所属分类: 4 工作手記8 網絡技術9 路由技术

公告

http://www.nuanbb.com

导航

常用链接

随笔分类(255)

常用链接