在std::vector下,不同記憶體配置方式效率簡單比較

來源:互聯網
上載者:User

#include "stdafx.h"<br />#include <iostream><br />#include <vector><br />#include <time.h><br />#include <boost/pool/pool_alloc.hpp><br />template<typename T><br />class alloc<br />{<br />public:<br />typedef std::size_t size_type;<br />typedef std::ptrdiff_t difference_type;<br />typedef T* pointer;<br />typedef const T* const_pointer;<br />typedef T& reference;<br />typedef const T& const_reference;<br />typedef T value_type;<br />public:<br /> alloc(){}<br /> template<typename T><br /> alloc(const alloc<T>& ){<br /> }<br /> alloc& operator=(const alloc& rhs){<br /> return *this;<br /> }<br /> static pointer allocate(size_type n)<br /> {<br /> return (pointer)malloc(n*sizeof(T));<br /> }<br />static void deallocate(pointer ptr, size_type n)<br />{<br />free((pointer)ptr);<br />}<br />size_type max_size() const throw()<br />{<br />return size_t(-1) / sizeof(T);<br />}<br />template <typename U><br />struct rebind<br />{<br />typedef alloc<U> other;<br />};</p><p>};<br />int _tmain(int argc, _TCHAR* argv[])<br />{ </p><p>int start4 = GetTickCount();<br />std::vector<int, alloc<int>> RR4;<br />for (int i = 0; i < 10000000; i++)<br />{<br />RR4.push_back(i);</p><p>}<br />int time4 = GetTickCount()- start4;<br />std::cout<<"alloc: "<<time4<<std::endl;<br />int start3 = GetTickCount();<br />std::vector<int, boost::fast_pool_allocator<int>> RR3;<br />for (int i = 0; i < 10000000; i++)<br />{<br />RR3.push_back(i);<br />}<br />int time3 = GetTickCount()- start3;<br />std::cout<<"boost::fast_pool_allocator: "<<time3<<std::endl;<br />int start1 = GetTickCount();<br />std::vector<int,std::allocator<int>> RR1;<br />for (int i = 0; i < 10000000; i++)<br />{<br />RR1.push_back(i);<br />}<br />int time1 = GetTickCount()- start1;<br />std::cout<<"std::allocator: "<<time1<<std::endl;</p><p>int start2 = GetTickCount();<br />std::vector<int, boost::pool_allocator<int>> RR2;<br />for (int i = 0; i < 10000000; i++)<br />{<br />RR2.push_back(i);<br />}<br />int time2 = GetTickCount()- start2;<br />std::cout<<"boost::pool_allocator: "<<time2<<std::endl;<br />return 0;<br />}

debug (VC2008):

alloc: 11438<br />boost::fast_pool_allocator: 19437<br />std::allocator: 11562<br />boost::pool_allocator: 27703<br />請按任意鍵繼續. . .

Release(VC2008):

alloc: 375<br />boost::fast_pool_allocator: 1032<br />std::allocator: 343<br />boost::pool_allocator: 1266<br />請按任意鍵繼續. . .

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.