您的位置:首页 > 编程语言

cpu时钟预取实例代码分享

2013-12-19 15:05 537 查看
#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;}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cpu时钟预取