利用内存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_tota
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值