10/08/2010

Code Kata的な 練習

どうもJudaです。

// Kata000.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"
#include
#include
#include
#include
#include


void foreachMethod(const size_t& size)
{
boost::progress_timer t;

using namespace boost::lambda;
using namespace std;
vector tmpArray(size);
tmpArray[50] = 200;

boost::progress_display show_progress( tmpArray.size());
for_each(
tmpArray.begin(),
tmpArray.end(),
(_1 + 1) );

}
void forMethod(const size_t& size)
{
boost::progress_timer t;

using namespace std;
vector tmpArray(size);
tmpArray[50] = 200;
boost::progress_display show_progress( tmpArray.size());
for( size_t i = 0; i < tmpArray.size(); i++){
int itt = tmpArray[i] + 1;
++show_progress;
//cout << tmpArray[i] << endl;
}
}
void foriterMethod(const size_t& size)
{
boost::progress_timer t;
using namespace std;
vector tmpArray(size);
tmpArray[50] = 200;

boost::progress_display show_progress( tmpArray.size());
for(
vector::const_iterator i = tmpArray.begin();
i != tmpArray.end(); i++){
int itt = *i + 1;
++show_progress;
//cout << *i << endl;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
const size_t size(1000000);
foreachMethod(size);
forMethod(size);
foriterMethod(size);
return 0;
}

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

0 件のコメント:

コメントを投稿