首页 / C++ / C++下快速内存拷贝
C++下快速内存拷贝
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++下快速内存拷贝,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2581字,纯文字阅读大概需要4分钟。
内容图文
#include<cstdio>
#include <cctype>
#include<cstdlib>
#include<cstring>
#include <time.h>
const int maxn = 1024 * 1024 * 1024 / 4;
struct Cons{
int i,j,d;
float w;
};
Cons *cons;
Cons *consCp;
int *Ci;
int *Cj;
int *Cd;
float *Cw;
int *CiCp;
int *CjCp;
int *CdCp;
float *CwCp;
void memcpyEach(){
std::memcpy(CiCp,Ci,maxn * sizeof(int));
std::memcpy(CjCp,Cj,maxn * sizeof(int));
std::memcpy(CdCp,Cd,maxn * sizeof(int));
std::memcpy(CwCp,Cw,maxn * sizeof(float));
}
void memcpyPage(){
int *pdstCi = CiCp;
int *psrcCi = Ci;
int *pdstCj = CjCp;
int *psrcCj = Cj;
int *pdstCd = CdCp;
int *psrcCd = Cd;
float *pdstCw = CwCp;
float *psrcCw = Cw;
for(int i = 0;i<maxn * sizeof(int);i+=4096){
std::memcpy(pdstCi,psrcCi,4096);
std::memcpy(pdstCj,psrcCj,4096);
std::memcpy(pdstCd,psrcCd,4096);
std::memcpy(pdstCw,psrcCw,4096);
pdstCi += 1024;
psrcCi += 1024;
pdstCj += 1024;
psrcCj += 1024;
pdstCd += 1024;
psrcCd += 1024;
pdstCw += 1024;
psrcCw += 1024;
}
}
void memcpyStruct(){
std::memcpy(consCp,cons,maxn * sizeof(Cons));
}
void memcpyPageStruct(){
Cons *pdstCons = consCp;
Cons *psrcCons = cons;
for(unsigned int i = 0;i< maxn;i+=4096 / sizeof(Cons)){
std::memcpy(pdstCons,psrcCons,4096);
pdstCons += 4096 / sizeof(Cons);
psrcCons += 4096 / sizeof(Cons);
}
}
void Malloc(){
Ci = (int*)std::malloc(maxn * sizeof(int));
Cj = (int*)std::malloc(maxn * sizeof(int));
Cd = (int*)std::malloc(maxn * sizeof(int));
Cw = (float*)std::malloc(maxn * sizeof(float));
CiCp = (int*)std::malloc(maxn * sizeof(int));
CjCp = (int*)std::malloc(maxn * sizeof(int));
CdCp = (int*)std::malloc(maxn * sizeof(int));
CwCp = (float*)std::malloc(maxn * sizeof(float));
cons = (Cons*)std::malloc(maxn * sizeof(Cons));
consCp = (Cons*)std::malloc(maxn * sizeof(Cons));
}
int main(){
clock_t start,end;
int timeuse;
start =clock();
Malloc();
end = clock();
timeuse = 1000 * (end - start) / CLOCKS_PER_SEC;
printf("Malloc:%d ms\n", timeuse);
start =clock();
memcpyStruct();
end = clock();
timeuse = 1000 * (end - start) / CLOCKS_PER_SEC;
printf("memcpyStruct:%d ms\n", timeuse);
start =clock();
memcpyEach();
end = clock();
timeuse = 1000 * (end - start) / CLOCKS_PER_SEC;
printf("memcpyEach:%d ms\n", timeuse);
start =clock();
memcpyPage();
end = clock();
timeuse = 1000 * (end - start) / CLOCKS_PER_SEC;
printf("memcpyPage:%d ms\n", timeuse);
start =clock();
memcpyPageStruct();
end = clock();
timeuse = 1000 * (end - start) / CLOCKS_PER_SEC;
printf("memcpyPageStruct:%d ms\n", timeuse);
return 0;
}
测试了在结构体下,以及分开存储。以及使用分页拷贝4KB的速度。结果显示,使用分页拷贝效率提高近10倍。
结果如下:
Malloc:0 ms
memcpyStruct:2115 ms
memcpyEach:2118 ms
memcpyPage:318 ms
memcpyPageStruct:293 ms
内容总结
以上是互联网集市为您收集整理的C++下快速内存拷贝全部内容,希望文章能够帮你解决C++下快速内存拷贝所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。