C++标准库(体系结构与内核分析)(侯捷video 7-?)

一、分配器(video7)

GNU C++编译器下测试多个allocator效率:

导入头文件:

#include <list>
#include <stdexcept>
#include <string>
#include <cstdlib>         //abort()
#include <cstdio>          //snprintf()
#include <algorithm>     //find()
#include <iostream>
#include <ctime> 

#include <cstddef>
#include <memory>    //內含 std::allocator  
//欲使用 std::allocator 以外的 allocator, 得自行 #include <ext\...> 
//以下allocator只能在GNUC g++中使用
#ifdef __GNUC__        
#include <ext\array_allocator.h>
#include <ext\mt_allocator.h>
#include <ext\debug_allocator.h>
#include <ext\pool_allocator.h>
#include <ext\bitmap_allocator.h>
#include <ext\malloc_allocator.h>
#include <ext\new_allocator.h>  
#endif
namespace zz12 {
    void use_allocator() {
        cout << "Test Allocators" << endl;

        list<long, allocator<long>> c1;                        
        list<long, __gnu_cxx::malloc_allocator<long>> c2;  
        list<long, __gnu_cxx::new_allocator<long>> c3;         
        list<long, __gnu_cxx::__pool_alloc<long>> c4;          
        list<long, __gnu_cxx::__mt_alloc<long>> c5;     
        list<long, __gnu_cxx::bitmap_allocator<long>> c6; 

        srand(time(0));

        clock_t timeStart = clock();
        for (long i = 0; i < MAX_LOOP; ++i){
            c1.push_back(rand());
        }
        cout << "allocator花费时间:" << clock() - timeStart << "ms" << endl;

        timeStart = clock();
        for (long i = 0; i < MAX_LOOP; ++i) {
            c2.push_back(rand());
        }
        cout << "malloc_allocator花费时间:" << clock() - timeStart << "ms" << endl;

        timeStart = clock();
        for (long i = 0; i < MAX_LOOP; ++i) {
            c3.push_back(rand());
        }
        cout << "new_allocator花费时间:" << clock() - timeStart << "ms" << endl;

        timeStart = clock();
        for (long i = 0; i < MAX_LOOP; ++i) {
            c4.push_back(rand());
        }
        cout << "__pool_alloc花费时间:" << clock() - timeStart << "ms" << endl;

        timeStart = clock();
        for (long i = 0; i < MAX_LOOP; ++i) {
            c5.push_back(rand());
        }
        cout << "__mt_alloc花费时间:" << clock() - timeStart << "ms" << endl;

        timeStart = clock();
        for (long i = 0; i < MAX_LOOP; ++i) {
            c6.push_back(rand());
        }
        cout << "bitmap_allocator花费时间:" << clock() - timeStart << "ms" << endl;
    }
}

二、直接使用分配器(video7)

在GNU C++编译器下测试直接使用不同的allocator分配空间以及deallocator归还空间:

namespace zz13 {
    void use_allocator_directly(){
        //test all allocators' allocate() & deallocate();
        int* p;
        allocator<int> alloc1;
        p = alloc1.allocate(1);
        alloc1.deallocate(p, 1);

        __gnu_cxx::malloc_allocator<int> alloc2;
        p = alloc2.allocate(1);
        alloc2.deallocate(p, 1);

        __gnu_cxx::new_allocator<int> alloc3;
        p = alloc3.allocate(1);
        alloc3.deallocate(p, 1);

        __gnu_cxx::__pool_alloc<int> alloc4;
        p = alloc4.allocate(2);    //分配2个int变量的空间
        alloc4.deallocate(p, 2);     //归还2个int变量的空间

        __gnu_cxx::__mt_alloc<int> alloc5;
        p = alloc5.allocate(1);
        alloc5.deallocate(p, 1);

        __gnu_cxx::bitmap_allocator<int> alloc6;
        p = alloc6.allocate(3);    //分配3个int变量的空间
        alloc6.deallocate(p, 3);      //归还3个
    }
}

不建议直接使用allocator,建议直接使用标准容器。如果需要分配小空间内存,那么建议使用熟悉的new、delete和malloc、free,因为这几个函数只需要关心分配的大小,而不用关心归还的大小,更方便,不容易出错。

三、(video7)

猜你喜欢

转载自www.cnblogs.com/leokale-zz/p/11107807.html
今日推荐