[C++] monotonic_buffer_resource
Updated:
개요
- 리소스가 소멸될 때만 할당된 메모리를 해제하는 특수 목적 메모리 리소스 클래스
- 빠른 메모리 할당 가능
- 쓰레드 세이프 하지 않음
예제
- 코드
#include <algorithm> #include <chrono> #include <iostream> #include <list> #include <memory_resource> using namespace std; int main() { auto declare_std = [](const int &size) { list<int> l; for (int i = 0; i < size; ++i) { l.push_back(i); } }; auto declare_pmr = [](const int &size) { pmr::list<int> l; for (int i = 0; i < size; ++i) { l.push_back(i); } }; auto declare_pmr_no_buffer = [](const int &size) { pmr::monotonic_buffer_resource mbr; pmr::polymorphic_allocator<int> pa{&mbr}; pmr::list<int> l{pa}; for (int i = 0; i < size; ++i) { l.push_back(i); } }; auto declare_pmr_buffer = [](const int &size) { // array<byte, numeric_limits<int>::max() / 200000> buffer; array<byte, 1024 * 1024> buffer; pmr::monotonic_buffer_resource mbr{buffer.data(), buffer.size()}; pmr::polymorphic_allocator<int> pa{&mbr}; pmr::list<int> l{pa}; for (int i = 0; i < size; ++i) { l.push_back(i); } }; auto run = [](auto func, const int &size) { chrono::system_clock::time_point start = chrono::system_clock::now(); func(size); chrono::duration<double> sec = chrono::system_clock::now() - start; cout << sec.count() << endl; return sec; }; const int size = 1000000; run(declare_std, size); run(declare_pmr, size); run(declare_pmr_no_buffer, size); run(declare_pmr_buffer, size); return 0; }
- 실행 결과
0.15664 0.164578 0.132461 0.130183