Thunder Bird
Communication & Improvement
posts - 47,  comments - 155,  trackbacks - 0
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 2009-11-25 16:44 Thunder 阅读(795) 评论(5)  编辑 收藏 引用

FeedBack:
# re: 大量字符串的拼接操作的耗时问题
2009-12-02 10:02 | 爱之谷
时间立刻降到百秒以内,效果十分明显
恩,验证了,不错  回复  更多评论
  
# re: 大量字符串的拼接操作的耗时问题
2009-12-10 11:02 | 99读书人俱乐部
是肯定卡数据库的健康  回复  更多评论
  
# re: 大量字符串的拼接操作的耗时问题
2009-12-12 13:44 | 喜乐递
阿隆索打算看见的  回复  更多评论
  
只有注册用户登录后才能发表评论。

<2009年11月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

常用链接

留言簿(8)

随笔档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜