STL中queue,list,vector频繁访问效率

针对STL中的queue,list,vector频繁访问,即插入(尾部)与取出(头部)操作时的效率进行了对比,从时间消耗上来看,queue最少,list与vector消耗相对较大,二者之间差别不大。附上类似分析一篇 :http://blog.youkuaiyun.com/lsldd/article/details/7857388。本文代码如下:

/*
 * msg.h
 * 
 *      Author: wdmcel
 */

#ifndef MSG_H_
#define MSG_H_


#include <iostream>
#include <queue>
#include <list>
#include <vector>
using namespace std ;

template <typename T>
class msg_queue_t
{
public :
	msg_queue_t(T data_)
   {
	   m_queue.push(data_);
	   m_excption = 0;
   }

   T pop_data()
   {
	   if(0 != get_size())
	   {
	   T data = m_queue.front();
	   m_queue.pop();
	   return data;
	   }
	   throw m_excption;
   }

   T get_first_data()
   {
	   if(0 != get_size())
	   {
	   return m_queue.front();
	   }
	   throw m_excption;
   }

   T get_last_data()
   {
	   if(0 != get_size())
	   {
	   return m_queue.back();
	   }
	   throw m_excption;
   }
   void insert_data(T c);

   int get_size()
   {
	   return m_queue.size();
   }

   void show_data()
   {
	   for(int i = 1; i <= get_size(); i++)
	   {
		   T data = pop_data();
		   cout << data << "\t";
		   insert_data(data);
	   }
	   cout << endl;
   }

private:
   queue <T> m_queue;
   int m_excption;
};

template <typename T>
void  msg_queue_t<T>::insert_data(T data_)
{
	m_queue.push(data_) ;
}

template <typename T>
class msg_list_t
{
public :
	msg_list_t(T data_)
   {
		m_list.push_back(data_);
		m_excption = 0;
   }

   T pop_data()
   {
	   if(0 != get_size())
	   {
	   T data = m_list.front();
	   m_list.pop_back();
	   return data;
	   }
	   throw m_excption;
   }

   T get_first_data()
   {
	   if(0 != get_size())
	   {
	   return m_list.front();
	   }
	   throw m_excption;
   }

   T get_last_data()
   {
	   if(0 != get_size())
	   {
	     return m_list.back();
	   }
	   throw m_excption;
   }

   void insert_data(T c);

   int get_size()
   {
	   return m_list.size();
   }

   void show_data()
   {
	   for(int i = 1; i <= get_size(); i++)
	   {
		   T data = pop_data();
	   	   cout << data << "\t";
	   	   insert_data(data);
	   }
	   cout << endl;
   }
private:
   list <T> m_list;
   int m_excption;
};

template <typename T>
void  msg_list_t<T>::insert_data(T data_)
{
	m_list.push_back(data_) ;
}

template <typename T>
class msg_vector_t
{
public :
	msg_vector_t(T data_)
   {
		m_vector.push_back(data_);
		m_excption = 0;
   }

   T pop_data()
   {
	   if(0 != get_size())
	   {
	   T data = m_vector.front();
	   m_vector.pop_back();
	   return data;
	   }
	   throw m_excption;
   }

   T get_first_data()
   {
	   if(0 != get_size())
	   {
		   return m_vector.front();
	   }
   	   throw m_excption;
    }

    T get_last_data()
    {
    	if(0 != get_size())
    	{
    		return m_vector.back();
    	}
   	   throw m_excption;
    }

   void insert_data(T c);

   int get_size()
   {
	   return m_vector.size();
   }

   void show_data()
   {
	   for (int i = 0; i < get_size(); i++)
	   {
		   cout << m_vector.at(i) << "\t";
	   }
	   cout << endl;
   }
private:
   list <T> m_vector;
   int m_excption;
};

template <typename T>
void  msg_vector_t<T>::insert_data(T data_)
{
	m_vector.push_back(data_) ;
}


#endif /* MSG_H_ */
测试代码(部分):

msg_queue_t<int> msg_queue_int(15);

		for (i = 1; i <= MOUNT; i++)
		{
			msg_queue_int.insert_data(test_int);
		}

		for (i = 1; i <= MOUNT; i++)
		{
			msg_queue_int.pop_data();
		}

		gettimeofday(&tvEnd, NULL);

		long ustime_queue = (tvEnd.tv_sec - tvStart.tv_sec) * 1000000 + tvEnd.tv_usec - tvStart.tv_usec;

		printf("ustime_queue:\t%ld\n", ustime_queue);

结果如本文开始所述,queue的底层 是对dequeue的适配,那么也就是分段内存实现存储,这样就不会像vector那样需要重新分配内存以及复制元素的操作了,但是list是双向链表,按说实现首尾的插入与删除应该也会很快,为何也相对queue而言,很慢呢?

难道如同,我在文章开头所引链接中所言:list的push_back的实现会导致内部使用大量动态内存分配,这个过程也相对耗时,所导致的结果么,求回复。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值