memcpy和memmove的意外性能 [英] Unexpected performance from memcpy and memmove

查看:114
本文介绍了memcpy和memmove的意外性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么memcpy在我的系统上执行得比memmove慢?

Why does memcpy perform slower than memmove on my system?

通过阅读其他SO问题,例如

From reading other SO questions such as this or this Gives the impression that memcpy should work faster than memmove, and intuitively, this should be so. After all, there are less checks that memcpy has and the man pages also match what they say.

但是,在测量每个函数内部花费的时间时,memmove胜过了memcpy!而且,当memset似乎可以从memcpy或memmove无法实现的优化中受益时,它似乎也击败了memset.为什么会这样呢?

However, when measuring the time spent inside of each function, memmove beats memcpy! What more, it seems to beat memset too, when memset seems like it could benefit from optimizations that memcpy or memmove can't. Why would this be so?

我的计算机上的结果(众多)之一:

Results (one of many) on my computer:

[INFO] (ex23.c:151 func: main) Normal copy: 109092
[INFO] (ex23.c:198 func: main) memcpy: 66070
[INFO] (ex23.c:209 func: main) memmove: 53149
[INFO] (ex23.c:219 func: main) memset: 52451

用于得出此结果的代码:

Code used to give this result:

#include <stdio.h>
#include <string.h>
#include "dbg.h" // debugging macros
#include <time.h>

int main(int argc, char *argv[])
{
    char from[10000] = {'a'};
    char to[10000] = {'c'};
    int rc = 0;
    struct timespec before; 
    memset(from, 'x', 10000);
    memset(to, 'y', 10000);

    clock_gettime(CLOCK_REALTIME, &before);

    // naive assignment using a for loop
    normal_copy(from, to, 10000);
    struct timespec after;
    clock_gettime(CLOCK_REALTIME, &after);
    log_info("Normal copy: %ld", (after.tv_nsec - before.tv_nsec));


    memset(to, 'y', 10000);
    clock_gettime(CLOCK_REALTIME, &before); 
    memcpy(to, from, 10000);
    clock_gettime(CLOCK_REALTIME, &after);
    log_info("memcpy: %ld", (after.tv_nsec - before.tv_nsec));

    memset(to, 'y', 10000);
    clock_gettime(CLOCK_REALTIME, &before);
    memmove(to, from, 10000);
    clock_gettime(CLOCK_REALTIME, &after);
    log_info("memmove: %ld", (after.tv_nsec - before.tv_nsec));

    memset(to, 'y', 10000);
    clock_gettime(CLOCK_REALTIME, &before);
    memset(to, 'x', 10000);
    clock_gettime(CLOCK_REALTIME, &after);
    log_info("memset: %ld", (after.tv_nsec - before.tv_nsec));

    return 0;
}

推荐答案

正如@Carl Norum和@Greg Hewgill所说:缓存效果.

As @Carl Norum and @Greg Hewgill say: cache effects.

您肯定会遇到缓存内存的影响.重新排列测试顺序并比较结果.当我在memmove()之前和之后测试memcpy()时,第二个memcpy()的表现与memove()相似,并且也比第一个memcpy()快.

Your certainly experiencing the effects of cached memory. Re-order your tests and compare results. When I tested memcpy() before and after memmove(), the 2nd memcpy() performed like memove() and also was faster than the first memcpy().

这篇关于memcpy和memmove的意外性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆