::NPTEAM:: Network Programer Team

검색 :
RSS 구독 : 글 / 댓글 / 트랙백 / 글+트랙백

[STL] Vector Container Iterating 속도 비교

std::vector container를 Iterating하는 방법은 여러가지가 존재합니다.

평소 컨테이너 Iterating은 크게 생각하지 않고 사용하던 중
for_each에 대한 속도 향상 관련 글을 읽고 도대체 얼마나 빨라지는지 테스트를 해 보았습니다.

방법 1. None Const Iterating
std::vector< int >::iterator it     = vecInteger.begin();
std::vector< int >::iterator it_end = vecInteger.end();

for( ; it != it_end; ++it )
{
	const int& nValue = (*it);
	/* Do Something */
}
방법 2. Const Iterating
std::vector< int >::const_iterator it     = vecInteger.begin();
std::vector< int >::const_iterator it_end = vecInteger.end();

for( ; it != it_end; ++it )
{
	const int& nValue = (*it);
	/* Do Something */
}
방법 3. std::for_each( Functor )를 사용함
struct stVecInteger
{ 
	void operator() (int nElement)
	{
		const int& nValue = nElement;
		/* Do Something */
	};
} stVecIntergerFunctor;

std::for_each( vecInteger.begin(), vecInteger.end(), stVecIntergerFunctor );
방법 4. BOOST_FOREACH
BOOST_FOREACH( int nElement, vecInteger )
{
	const int& nValue = nElement;
	/* Do Something */
};
방법 5. for each(element in Container)
for each( int nElement in vecInteger )
{
	const int& nValue = nElement;
	/* Do Something */
};

Vector 컨테이너에 int 자료형을 1억개 넣고, Iterating 했을때의 결과(Release 컴파일)

Release 모드로 컴파일 했을때,
Functor가 최적화되어 어셈블리 코드가 없을때에는 0의 실행시간을 보여주는 엄청난 결과를 알게 되었다.

Functor에 별도의 수행 코드가 추가되더라도 가장 빠른 수행 성능을 보여주었다.
2010/03/01 21:32 2010/03/01 21:32

맨 위로