栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

cpu时钟预取实例代码分享

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cpu时钟预取实例代码分享

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

复制代码 代码如下:
#include

#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:%ldn", end - start);


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

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

    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/37825.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号