memmove 对同一个指针不操作,所以调用memmove之前不用比较两个指针是否相同
void CTestDLLDlg::OnBnClickedButton6() {const int size = 999999;char* data = new char[size];memset(data, 1, size - 1);char* data1 = new char[size];memset(data1, 'a', size - 1);clock_t begin = clock();for (int i = 0; i < size; ++i){memmove(data1, data, size);}clock_t end = clock();double duration = 0;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 = 0; i < size; ++i){memmove(data1, data1, size);}clock_t end1 = clock();double duration1 = 0;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 = 0; i < size; ++i){}clock_t end2 = clock();double duration2 = 0;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;}
显示结果是:
-不同指针复制- 49.971000---
-同指针复制- 38.833000---
-空循环- 38.833000---
将memmove换成memcpy,显示
-不同指针复制- 49.782000---
-同指针复制- 38.847000---
-空循环- 38.847000---
没觉得memcpy有多快