10/08/2010

Code Kata的な 練習

どうもJudaです。
  1. // Kata000.cpp : コンソール アプリケーションのエントリ ポイントを定義します。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <vector>  
  7. #include <algorithm>  
  8. #include <boost lambda="" lambda.hpp="">  
  9. #include <boost progress.hpp="">  
  10.   
  11.   
  12. void foreachMethod(const size_t& size)  
  13. {  
  14.   boost::progress_timer t;  
  15.   
  16.   using namespace boost::lambda;  
  17.   using namespace std;  
  18.   vector<int> tmpArray(size);  
  19.   tmpArray[50] = 200;  
  20.   
  21.   boost::progress_display show_progress( tmpArray.size());  
  22.   for_each(  
  23.     tmpArray.begin(),  
  24.     tmpArray.end(),  
  25.     (_1 + 1) );  
  26.   
  27. }  
  28. void forMethod(const size_t& size)  
  29. {  
  30.   boost::progress_timer t;  
  31.   
  32.   using namespace std;  
  33.   vector<int> tmpArray(size);  
  34.   tmpArray[50] = 200;  
  35.   boost::progress_display show_progress( tmpArray.size());  
  36.   forsize_t i = 0; i < tmpArray.size(); i++){  
  37.     int itt = tmpArray[i] + 1;  
  38.     ++show_progress;  
  39.     //cout << tmpArray[i] << endl;  
  40.   }  
  41. }  
  42. void foriterMethod(const size_t& size)  
  43. {  
  44.   boost::progress_timer t;  
  45.   using namespace std;  
  46.   vector<int> tmpArray(size);  
  47.   tmpArray[50] = 200;  
  48.     
  49.   boost::progress_display show_progress( tmpArray.size());  
  50.   for(   
  51.     vector<int>::const_iterator i = tmpArray.begin();  
  52.     i != tmpArray.end(); i++){  
  53.     int itt = *i + 1;  
  54.     ++show_progress;  
  55.     //cout << *i << endl;  
  56.   }  
  57. }  
  58.   
  59. int _tmain(int argc, _TCHAR* argv[])  
  60. {  
  61.   const size_t size(1000000);  
  62.   foreachMethod(size);  
  63.   forMethod(size);  
  64.   foriterMethod(size);  
  65.  return 0;  
  66. }  
  67. </int></int></int></int></boost></boost></algorithm></vector></iostream>  

とりあえずいえるのは、VS2008のReleaseならforeachがはやい。Debugならforが早い。とりあえずどれでもイテレータは遅い。でもこの場合に重要な点は、こんな単純なケースで通常のイテレータ回しはあまり有用ではない、という点と、イテレータでの制御はもっと凝ったもののほうがいいかもしれない。とりあえずforeachすげー。投げる要素数が増えるとかなり露骨に早くなる。

0 件のコメント:

コメントを投稿