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有多快
?