基础知识通道:http://blog.csdn.net/Xiejingfa/article/details/50955295

C/C++:

 #include <iostream>
#include <vector>
#include <string> #define allocate_length 100000 int main()
{ //allocator比new快的原因:分离分配和初始化这两个操作allocator少执行一步,new则一般执行两次(初始化和赋值); std::clock_t start = , end = ; start = clock();
std::string *str1 = new std::string[allocate_length];
auto str6=str1;
for (int i = ; i < allocate_length; i++)
{
*str1++ = "Hello World";
} delete []str6;
end = clock();
std::cout << (double(end - start) / CLOCKS_PER_SEC) << std::endl; start = clock();
std::allocator<std::string> str_allocate;
std::string *str3 = str_allocate.allocate(allocate_length); //分配20个string原始内存
auto str4=str3; //方法一:使用默认构造
for (int i = ; i < allocate_length; i++)
{
str_allocate.construct(str3++,"Hello World");
} //方法二:使用allocator的伴随算法(分别是带n与不带)
//其他算法:std::uninitialized_copy(iterator begin,iterator end,T value); //std::uninitialized_fill_n(str3,allocate_length,"Hello World"); //str_allocate.destroy()调用对象的析构,但是内存还是由allocator控制,需要自己释放
str_allocate.deallocate(str4,allocate_length);
end = clock(); std::cout << (double(end - start) / CLOCKS_PER_SEC) << std::endl; return ;
}

C++ std::allocator<T> 与new对比效率使用的更多相关文章

  1. no suitable ctr exists to convert from 'int' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char> >

    int xfun(int *a,int n) { int x = *a;//a的类型是int *,a+1跳动一个int的长度 ; pa < a + n; pa++)//指向同一个类型的指针比较大 ...

  2. 浅谈C++ allocator内存管理(对比new的局限性)(转)

    STL中,对内存管理的alloc的设计,迫使我去学习了allocator类.这里对allocator内存管理做了点笔记留给自己后续查阅.allocator类声明.定义于头文件<memory> ...

  3. gcc5.4报错对‘std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()’未定义的引用

    我在编译ligra是遇到了这个问题,网上搜了一遍,发现是了原因https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/manual/using_dual_abi.ht ...

  4. 【CString与string转换】不存在从 "LPWSTR" 转换到 "std::basic_string<char, std::char_traits<char>, std::allocator(转)

    原文转自 http://blog.csdn.net/qq_23536063/article/details/52291332 [问题描述] CString cstr: sring str(cstr.G ...

  5. libraries\include\boost-1_61\boost/regex/v4/perl_matcher.hpp(362): error C2292: 'boost::re_detail_106100::perl_matcher<const char *,std::allocator<boost::sub_match<const char *>>,boost::regex_traits<c

    这个问题在Windows上基于CMake编译Caffe-SSD的GPU版时出现. 网上找到的博客贴出的解决办法是删掉regex和rv相关代码,甚至不编译detection_output_layer.c ...

  6. hive 连接查询sql对比效率

    准备4个表 从mysql 导出excel 转换为txt 创建hive 表的导入文件 create table bdqn_student( sno int, sname string, sbirthda ...

  7. error C3867: “std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str”: 函数调用缺少参数列表;请使用“&std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str”创建指向成员的指针

    这个问题找了很多没有找到满意的答案.仔细看了一下,是使用了c_str的问题. 我直接把使用string.c_str的地方使用char*代替即解决问题.

  8. 实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  9. [转载] 彻底学习STL中的Allocator

    原文: http://cissco.iteye.com/blog/379093 帮助我们理解allocator的原理 Allocator是C++语言标准库中最神秘的部分之一.它们很少被显式使用,标准也 ...

随机推荐

  1. host命令详解

    基础命令学习目录首页 原文链接:https://blog.csdn.net/xin_y/article/details/53924763 分析域名查询工具,测试域名系统工作是否正常 语法: host ...

  2. Apache 工作模式的正确配置

       prefork work event

  3. MVC 漫长之路(一)

    1.新建项目 mvc  视图引擎选中 Razor   2.允许我们设置这个项目关于 MVC 的一些设置,确认选中了“空”项目模板   3.   4.打开 Global.asax 文件 配置路由名称等 ...

  4. sprint2(第六天)

    昨天休息一天,今天继续做任务. 燃尽图:

  5. HDU 4405 Aeroplane chess 期望dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...

  6. 韩剧TV APP案例分析

    产品 选择产品:韩剧TV 版本:Android版 选择理由:节假日坐车回家时使用较多次数的APP,刚好国庆坐车回家时正在使用,所以选择了这款APP. 第一部分:调研.评测 第一次上手体验 刚打开APP ...

  7. nigix安装

    树莓派安装nginx,参考http://blog.csdn.net/zizi7/article/details/54347223 1. 下载PCRE 并安装. 主页地址: ftp://ftp.csx. ...

  8. DispatcherServlet的作用

    DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好 ...

  9. Mongodb 分片操作 介绍

    为什么需要分片操作?由于数据量太大,使得CPU,内存,磁盘I/O等压力过大.当MongoDB存储海量的数据时,一台机器可能不足以存储数据,也可能不足以提供可接受的读写吞吐量.这时,我们就可以通过在多台 ...

  10. beta——5

    一.提供当天站立式会议照片一张: 二. 每个人的工作 (有work item 的ID) (1) 昨天已完成的工作: 对用户功能的添加. (2) 今天计划完成的工作: web发布 (3) 工作中遇到的困 ...