相关疑难解决方法(0)

为什么malloc + memset比calloc慢?

众所周知,它与初始化分配的内存calloc不同malloc.使用时calloc,内存设置为零.使用时malloc,内存不会被清除.

所以在日常工作中,我认为callocmalloc+ memset.顺便说一下,为了好玩,我为基准编写了以下代码.

结果令人困惑.

代码1:

#include<stdio.h>
#include<stdlib.h>
#define BLOCK_SIZE 1024*1024*256
int main()
{
        int i=0;
        char *buf[10];
        while(i<10)
        {
                buf[i] = (char*)calloc(1,BLOCK_SIZE);
                i++;
        }
}
Run Code Online (Sandbox Code Playgroud)

代码1的输出:

time ./a.out  
**real 0m0.287s**  
user 0m0.095s  
sys 0m0.192s  
Run Code Online (Sandbox Code Playgroud)

代码2:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BLOCK_SIZE 1024*1024*256
int main()
{
        int i=0;
        char *buf[10];
        while(i<10)
        {
                buf[i] = (char*)malloc(BLOCK_SIZE);
                memset(buf[i],'\0',BLOCK_SIZE);
                i++;
        }
}
Run Code Online (Sandbox Code Playgroud)

代码2的输出:

time ./a.out   
**real 0m2.693s**  
user 0m0.973s  
sys 0m1.721s  
Run Code Online (Sandbox Code Playgroud)

更换 …

c malloc

249
推荐指数
2
解决办法
5万
查看次数

英特尔TBB的Scalable_allocator如何工作?

什么是tbb::scalable_allocator英特尔线程构建模块实际上是引擎盖下呢?

它肯定是有效的.我刚用它来采取25%的折扣的应用程式的执行时间(并看到在CPU利用率从〜200%4核系统上增加至350%),通过改变单个std::vector<T>std::vector<T,tbb::scalable_allocator<T> >.另一方面,在另一个应用程序中,我看到它将已经很大的内存消耗加倍并将内容发送到交换城市.

英特尔自己的文档并没有给出太多帮助(例如本常见问题解答末尾的简短部分 ).在我自己去挖掘代码之前,谁能告诉我它使用了什么技巧?

更新:刚刚第一次使用TBB 3.0,并且从Scalable_allocator看到了我最好的加速.改变单一vector<int>vector<int,scalable_allocator<int> >(从测试的Debian莱尼,核2,与TBB 3.0)的从85S东西35S减少的运行时间.

c++ malloc multithreading stl tbb

27
推荐指数
1
解决办法
2万
查看次数

标签 统计

malloc ×2

c ×1

c++ ×1

multithreading ×1

stl ×1

tbb ×1