4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

STL vector・list全走査速度

Posted at

自分用、ソースの説明は放棄

実行環境
OS:Windows 7 Home Premium
CPU:Core i7-2700K
RAM:16GB

検証クラス。

class cObj
{
private:
	bool m_Flag;
	double m_X, m_Y;
	double m_Angle, m_Speed;
	int m_Frame;

public:
	virtual bool Update()
	{
		m_Frame= Rand( 60, 120 );
		return false;
	}
	virtual void DrawProcess(){}
};

要素は65535個で回す、なんとなく

const int MAX_SIZE = 65535;

測定方法

	__int64	times;
	__int64 timeAvg = 0; 
	__int64 cntAvg = 0; 

	while( 1 )
	{
		times = GetNowHiPerformanceCount();

		// ***** ここに処理部分 *****
			
		times = GetNowHiPerformanceCount() - times;
		timeAvg += times;
		cntAvg ++;

		// 平均時間 = timeAvg / cntAvg;
	}

#vector全走査

std::vector< cObj > m_Vec;
m_Vec.resize( MAX_SIZE );
	//	50.0ms
	for( auto it : m_Vec )
	{
		it.Update();
	}

	//	20.6ms
	for( auto &it : m_Vec )
	{
		it.Update();
	}

	//	19.6ms
	unsigned max = m_Vec.size();
	for( unsigned i=0 ; i<max ; i++ )
	{
		m_Vec[ i ].Update();
	}

	//	29.8ms
	for( unsigned i=0 ; i<m_Vec.size() ; i++ )
	{
		m_Vec[ i ].Update();
	}

	//	30.4ms
	for( unsigned i=0 ; i<m_Vec.size() ; i++ )
	{
		m_Vec.at( i ).Update();
	}

	//	54.6ms
	for( auto it=m_Vec.begin() ; it!=m_Vec.end() ; ++it )
	{
		it->Update();
	}

	//	24.6ms
	auto end = m_Vec.end();
	for( auto it=m_Vec.begin() ; it!=end ; ++it )
	{
		it->Update();
	}

#list全走査

std::list< cObj > m_List( MAX_SIZE );
	//	51.2ms
	for( auto it : m_List )
	{
		it.Update();
	}

	//	21.9ms
	for( auto &it : m_List )
	{
		it.Update();
	}

	//	60.7ms
	for( auto it=m_List.begin() ; it!=m_List.end() ; ++it )
	{
		it->Update();
	}

	//	26.2ms
	auto end = m_List.end();
	for( auto it=m_List.begin() ; it!=end ; ++it )
	{
		it->Update();
	}

#おまけ、forward_List全走査
listより軽いと思ってやったら全然重かった、ダメ

std::forward_list< cObj > m_fList( MAX_SIZE );
	//	55.7ms
	for( auto it : m_fList )
	{
		it.Update();
	}

	//	26.1ms
	for( auto &it : m_fList )
	{
		it.Update();
	}

	//	60.0ms
	for( auto it=m_fList.begin() ; it!=m_fList.end() ; ++it )
	{
		it->Update();
	}

	//	31.6ms
	auto end = m_fList.end();
	for( auto it=m_fList.begin() ; it!=end ; ++it )
	{
		it->Update();
	}

#まとめ

endは重い
sizeも意外と重い
範囲forは&をつけないと重い

vectorが爆速かと思ってたけど、listも意外と悪くない

4
3
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?