memmove 對同一個指針不操做

memmove 對同一個指針不操做,因此調用memmove以前不用比較兩個指針是否相同spa

 

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---code

將memmove換成memcpy,顯示orm

-不一樣指針複製- 49.782000---
-同指針複製- 38.847000---
-空循環- 38.847000---blog

 

沒以爲memcpy有多快io

相關文章
相關標籤/搜索