测试下预取的效果,利用CPU始终查看效果。根据实验发现,预取地址在地址使用之前的十行左右代码处效果比较好! #include <stdio.h> #define MAX_LEN 1000000
测试下预取的效果,利用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:%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;
}
--结束END--
本文标题: cpu时钟预取实例代码分享
本文链接: https://www.lsjlt.com/news/17984.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-13
2024-05-13
2024-05-11
2024-05-11
2024-05-10
2024-05-07
2024-04-30
2024-04-30
2024-04-30
2024-04-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0