码迷,mamicode.com
首页 > 其他好文 > 详细

memmove 对同一个指针不操作

时间:2015-12-26 11:45:14      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

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

 

memmove 对同一个指针不操作

标签:

原文地址:http://www.cnblogs.com/XiHua/p/5077728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!