信息发布→ 登录 注册 退出

cpu时钟预取实例代码分享

发布时间:2026-01-11

点击量:

测试下预取的效果,利用CPU始终查看效果。根据实验发现,预取地址在地址使用之前的十行左右代码处效果比较好!

复制代码 代码如下:
#include <stdio.h>

#define MAX_LEN 1000000

static inline void prefetchnta(void *addr) //预取部分
{
    __asm__("movl %0, %%eax"::"a"(addr));
    __asm__(".byte 0x0f, 0x18, 0x00");
}

inline unsigned long long GetCPUTickCount()
{
    unsigned long high32 = 0;
    unsigned long low32 = 0;

    __asm__("RDTSC" : "=a"(low32), "=d"(high32));

    unsigned long long counter = high32;
    counter = (counter<<32) + low32;

    return counter;
}

int main(int argc, char* argv[])
{
    long long start, end;
    long long array[MAX_LEN];
    int i;

    for(i = 0; i < MAX_LEN; i++) //让cache失效
        array[i]++;

    start = GetCPUTickCount();
    array[0]++;
    end = GetCPUTickCount();
    printf("don't use prefetch time:%ld\n", end - start);


    for(i = 0; i < MAX_LEN; i++)
        array[i]++;

    prefetchnta(array);
    start = GetCPUTickCount();
    array[0]++;
    end = GetCPUTickCount();
    printf("use prefetch time:%ld\n", end - start);

    return 0;
}

标签:# eax  # stdio  # define  # MAX_LEN  # lt  # CPU  # 下预取  # span  # include  # BR  # movl  # cpu时钟预取  # x0f  # byte  # nbsp  # inline  # static  # void  # addr  # prefetchnta  # 比较好  
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!