memmove 对同一个指针不操作

1、memmove 对同一个指针不操作,所以调用memmove之前不用比较两个指针是否相同

2、使用memmove是比memcpy更安全的

3、https://www.cnblogs.com/luoquan/p/5265273.html

void CTestDLLDlg::OnBnClickedButton6()
{
const int size = ;
char* data = new char[size];
memset(data, , size - ); char* data1 = new char[size];
memset(data1, 'a', size - ); clock_t begin = clock();
for (int i = ; i < size; ++i)
{
memmove(data1, data, size);
}
clock_t end = clock(); double duration = ;
duration = (double)(end - begin) / CLOCKS_PER_SEC;
CString str = _T("");
str.Format(_T("-不同指针复制- %f---\n"), duration);
OutputDebugString(str); clock_t begin1 = clock();
for (int i = ; i < size; ++i)
{
memmove(data1, data1, size);
}
clock_t end1 = clock(); double duration1 = ;
duration1 = (double)(end1 - begin1) / CLOCKS_PER_SEC;
CString str1 = _T("");
str1.Format(_T("-同指针复制- %f---\n"), duration1);
OutputDebugString(str1); clock_t begin2 = clock();
for (int i = ; i < size; ++i)
{
}
clock_t end2 = clock(); double duration2 = ;
duration2 = (double)(end1 - begin1) / CLOCKS_PER_SEC;
CString str2 = _T("");
str2.Format(_T("-空循环- %f---\n"), duration2);
OutputDebugString(str2); delete data;
data = nullptr; delete data1;
data1 = nullptr; }

显示结果是:

memmove 对同一个指针不操作

-不同指针复制- 49.971000---
-同指针复制- 38.833000---
-空循环- 38.833000---

将memmove换成memcpy,显示

-不同指针复制- 49.782000---
-同指针复制- 38.847000---
-空循环- 38.847000---

没觉得memcpy有多快

上一篇:J2ee的13个规范


下一篇:MR案例:输出/输入SequenceFile