C++ std::allocator<T> 与new对比效率使用
基础知识通道: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对比效率使用的更多相关文章
- 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++)//指向同一个类型的指针比较大 ...
- 浅谈C++ allocator内存管理(对比new的局限性)(转)
STL中,对内存管理的alloc的设计,迫使我去学习了allocator类.这里对allocator内存管理做了点笔记留给自己后续查阅.allocator类声明.定义于头文件<memory> ...
- 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 ...
- 【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 ...
- 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 ...
- hive 连接查询sql对比效率
准备4个表 从mysql 导出excel 转换为txt 创建hive 表的导入文件 create table bdqn_student( sno int, sname string, sbirthda ...
- 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*代替即解决问题.
- 实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)
首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下: C++ Code 1 2 template < class _Ty, cl ...
- [转载] 彻底学习STL中的Allocator
原文: http://cissco.iteye.com/blog/379093 帮助我们理解allocator的原理 Allocator是C++语言标准库中最神秘的部分之一.它们很少被显式使用,标准也 ...
随机推荐
- Excel VBA宏 链接服务器 上传和下载数据
首先说明以下. 第一: 下面的 “ _" 也就是 空格下划线 在VBA中表示换行的意思:& 表示链接连个字符串的操作,注意 & 的前后是否需要空格 第二: 如果链接服务器,服 ...
- 20162328蔡文琛week08
学号 20162328 <程序设计与数据结构>第X周学习总结 教材学习内容总结 错误和异常代表不常见的或不正确处理的对象. 抛出异常时输出的消息提供了方法调用栈的轨迹. 每个catch子句 ...
- Task 6.2冲刺会议七 /2015-5-20
今天把主界面改善了一下,主要功能时摄像头的使用以及语音聊天的部分,两个部分的设计原理基本相同,但是可是好几个模块让有点不知道该怎么下手.这时候我感觉到了模块拼接是很困难的,只有十分清楚每个模块才能很好 ...
- Task 6.2冲刺会议四 /2015-5-17
今天主要是学习并熟悉了C#的开发流程,把他的文件的大体结构和每个组件之间的联系弄清楚之后.开始写服务器部分的内容.学习过程中,感觉网上的资料有些太鱼龙混杂了,不知道该怎么取舍.明天准备完善服务器的功能 ...
- 从高版本JDK换成低版本JDK报错Unsupported major.minor version 52.0的解决方案
从高版本JDK换成低版本JDK报错Unsupported major.minor version 52.0 java.lang.UnsupportedClassVersionError: PR/Sor ...
- java属性编辑器,即PropertyEditor
出处:http://www.iteye.com/topic/1123628
- [图的遍历&多标准] 1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...
- 关于MUI v0.18.0版本 Table组件里的复选框不能选的解决方案
前段时间在用MUI的时候,Table组件出现复选框不能选的bug(描述: 点击复选框,点击事件会触发,复选框勾选状态无变化). 解决方法: 用CheckBox组件代替Table组件自带的复选框. 解决 ...
- Workstation和Virtualbox的虚拟机磁盘扩容方式.
1. 虚拟机磁盘管理, 更改磁盘格式是一个场景 还有一个场景是 硬盘空间不够了 需要扩充. 方法主要有两个. 如果是workstation的的虚拟机. 并且没有快照 可以直接GUI操作 如下图: 虚拟 ...
- Linux下的网卡Bonding
1. 网卡Bonding一共有0-6七种mode,具体区别请自行搜索: 2. 建议通过nmtui命令在交互模式下配置,这样不会落下重要的字段,也不用去记忆到底有哪些字段: 3. 我的实验环境是VMWa ...