利用內存chunk充當數據buffer的vector的實現,和STL vector 有接口操作性能比較

問題描述:

1.vector是最常用到的容器,它其實是一種自動擴充的動態數組,底層實現通過兩倍擴展,

   所以再不能預知要存入元素多少時,難免要兩倍擴展,這帶來了拷貝所存對象的開銷;

2.本文嘗試利用memory chunk作爲底層存儲容器來避免動態擴展時copy 開銷;

3.本實現屏蔽了STL  vector 一些接口比如erase,主要是爲了減輕實現複雜性考慮;

4.它支持兩邊插入和刪除操作,其接口功能完全等同於STL 的deque;

5.和STL vector 做了push_back, pop_back, at 操作性能比較,結果如下:

    1)push_back 操作,性能改善了4-5倍;

    2)at操作,基本上相當;

    3)pop_back性能變慢了3-4倍;

    4)  儘管pop_back性能變慢了,但是我的實現內存大小是可伸縮的;


程序代碼:


#ifndef _ALVECTOR_H_
#define _ALVECTOR_H_

#include <vector>

#include "windows.h"


/*
* the class encapsulate dynamic array the same as STL vector from function
* it have different underlying storage with STL vector
* it apply to memory chunk tech linked the chunk by next point
* it have good insert performance because no copy burden when full storage
* 
*/
template<class T>
class AlVector
{
public:
	/*
	* memory chunk(storage chunk) for interval using 
	*
	*/
	enum 
	{
		KSlotCount = 64
	};

	int m_totalCount;
	int m_count;

	struct Buffer
	{
		T        buf[KSlotCount];
		Buffer*  next;

		Buffer():next(0)
		{
			memset( buf, 0x00, sizeof(buf) );
		}
	};




	/*
	* Constructor
	*
	*/
	AlVector():m_totalCount(0), m_count(0),
		       m_chunkHead(0), m_chunkSize(0), 
			   m_chunkCapacity(0)
	{
		InitChunkIndex();
		m_dataBuffer = &m_initBuffer;
		AssignChunkIndex( m_dataBuffer );
	}


	/*
	* Copy Constructor
	*
	*/
	AlVector( const AlVector& rhs )
	{
		size_t len = rhs.Size();
		for( size_t i = 0; i < len; i++ )
			this->PushBack( rhs[i] );
	}


	/*
	* Assignment operator overload
	*
	*/
	AlVector& operator = ( const AlVector& rhs )
	{
		if( this != &rhs )
		{
			Clear();
			size_t len = rhs.Size();
			for( size_t i = 0; i < len; i++ )
				this->PushBack( rhs[i] );

		}

		return *this;
	}


	/*
	* Destructor
	*
	*/
	~AlVector()
	{
		Clear();
	}


	/*
	*
	*
	*/
	T& operator [] ( size_t index )
	{
		return At( index );
	}


	/*
	*
	*
	*/
	const T& operator[] ( size_t index ) const 
	{
		return At( index );
	}


	/*
	* Check whether or not exist element
	*
	*/
	bool IsEmpty()
	{
		return m_totalCount == 0;
	}


	/*
	* Retrieve the number of elements in container
	*
	*/
	size_t Size() const 
	{
		return m_totalCount;
	}

	/*
	* Clear all object pushed and memory
	*
	*/
	void Clear()
	{
		while( m_dataBuffer != &m_initBuffer )
		{
			Buffer* buf = m_dataBuffer;
			m_dataBuffer = m_dataBuffer->next;
			delete buf;			
		}

		delete [] m_chunkHead;
		m_chunkHead = 0;
		m_chunkSize = m_chunkCapacity = 0;
	}


	/*
	* Push element to container tail 
	*
	*/
	void PushBack( const T& data )
	{
		*Push() = data;
	}


	/*
	* Pop element from container tail
	*
	*/
	void PopBack()
	{
		m_totalCount--;
		if( --m_count == 0 )
		{
			if( m_dataBuffer != &m_initBuffer )
			{
				Buffer* buf = m_dataBuffer;
				m_dataBuffer = m_dataBuffer->next;

				//important
				m_chunkHead[m_chunkSize] = 0;
				m_chunkSize--;

				delete buf;
				buf = 0;

				// reset the value of m_count
				m_count = KSlotCount;
			}
		}
	}


	/*
	* Retrieve the element of header in container
	*
	*/
	T& Front()
	{
		return m_chunkHead[0]->buf[0];
	}

	/*
	* Retrieve the element of header in container
	*
	*/
	const T& Front() const
	{
		return Front();
	}


	/*
	* Retrieve the element of tail in container
	*
	*/
	T& Back()
	{
		return Back()
	}

	/*
	* Retrieve the element of tail in container
	*
	*/
	const T& Back() const 
	{
		assert( m_count > 0 );
		return m_dataBuffer[ m_count - 1 ];
	}


	/*
	* Retrieve the element by terms of position index
	*
	*/
	T& At( size_t index )
	{
		assert( index < m_totalCount );
		if( index < KSlotCount )
			return m_initBuffer.buf[index];

		return m_chunkHead[index / KSlotCount]->buf[index % KSlotCount];
	}


	/*
	* Retrieve the element by terms of position index
	*
	*/
	const T& At( size_t index ) const 
	{
		assert( index < m_totalCount );
		if( index < KSlotCount )
			return m_initBuffer.buf[index];

		return m_chunkHead[index / KSlotCount]->buf[index % KSlotCount];
	}

protected:

	/*
	*
	*
	*/
	T* Push()
	{
		if( KSlotCount == m_count )
		{
			Buffer* buf = new Buffer;
			assert( buf );
			buf->next = m_dataBuffer;
			m_dataBuffer = buf;

			//important
			AssignChunkIndex( buf );


			//reset the value of value in single buffer
			m_count = 0;
		}

		m_totalCount++;
		return &m_dataBuffer->buf[m_count++];
	}

	/*
	* Init the array of index of buffer pointer 
	*
	*/
	void InitChunkIndex()
	{
		m_chunkCapacity = KSlotCount;
		m_chunkHead = new Buffer*[m_chunkCapacity];
		memset( m_chunkHead, 0x00, sizeof(Buffer*)* m_chunkCapacity );
		m_chunkSize;
	}


	/*
	* Store currently buffer pointer to array of index 
	*
	*/
	void AssignChunkIndex( Buffer* curBuffer )
	{
		if( m_chunkSize >= m_chunkCapacity )
		{
			m_chunkCapacity += 2;// important
			m_chunkCapacity <<= 1;
			Buffer** newHead = new Buffer*[ m_chunkCapacity ];
			memset( newHead, 0x00, sizeof(Buffer*)* m_chunkCapacity );

			if( m_chunkHead )
			{
				memcpy( newHead, m_chunkHead, sizeof(Buffer*) * m_chunkSize );
				delete [] m_chunkHead;
			}

			m_chunkHead = newHead;
		}

		m_chunkHead[m_chunkSize++] = curBuffer;
	}

protected:


	Buffer     m_initBuffer; // static storage for a small amount object operation
	Buffer*    m_dataBuffer; // currently the pointer of buffer of storage

	Buffer**    m_chunkHead;  // the array of index of buffer of storage allocated
	size_t      m_chunkSize;  // the length of array of index of buffer
	size_t      m_chunkCapacity; // the capacity of ...

};

/*
* Test STL vector
*
*/
void TestSTLVector()
{
	std::vector<int> stlVec;
	int len = 64 * 100000;

	printf("STL Vector test result : \n " );
	unsigned long start= GetTickCount();
	for( int i = 0; i < len; i++ )
	{
		stlVec.push_back( i );
	}
	unsigned long interval = GetTickCount() - start;
	printf( "insert operation consume time is %d  in STL vector\n", interval );

	start= GetTickCount();
	for( int i = 0; i < len; i++ )
	{
		assert( stlVec[i] == i );
	}
	interval = GetTickCount() - start;
	printf( "retrieve operation consume time is %d  in STL vector\n", interval );



    start= GetTickCount();
	for( int i = 0; i < len; i++ )
	{
		stlVec.pop_back();
	}
	interval = GetTickCount() - start;
	printf( "pop operation consume time is %d  in STL vector\n", interval );

	assert( stlVec.size() == 0 );


}

/*
* Test AlVector
*
*/
void TestVector()
{
	int len = 64 * 100000;
	AlVector<int> vecObj;

	printf("AlVector Vector test result : \n " );
	unsigned long start= GetTickCount();
	for( int i = 0; i < len; i++ )
	{
		vecObj.PushBack( i );
	}
	unsigned long interval = GetTickCount() - start;
	printf( "insert operation consume time is %d  in AlVector \n", interval );

	start= GetTickCount();
	for( int i = 0; i < len; i++ )
	{
		assert( vecObj[i] == i );
	}
	interval = GetTickCount() - start;
	printf( "retrieve operation consume time is %d  in AlVector\n", interval );


	start= GetTickCount();
	for( int i = 0; i < len; i++ )
	{
		vecObj.PopBack();
	}
	interval = GetTickCount() - start;
	printf( "pop operation consume time is %d  in AlVector\n", interval );

	assert( vecObj.Size() == 0 );


	vecObj.Clear();
}


/*
* Test interface 
*
*/
void TestVectorSuite()
{
	TestVector();
	TestSTLVector();

}

#endif 

compile and run in visual studio 2005
test result as follows:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章