How to write cpu clock prefetch code
This article mainly explains "how to write cpu clock prefetch code". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to write cpu clock prefetch code".
Test the effect of prefetching and always check the effect with CPU. According to the experiment, it is found that the prefetching address works better at about ten lines of code before the address is used.
The code is as follows:
# include
# define MAX_LEN 1000000
Static inline void prefetchnta (void * addr) / / prefetch part
{
_ _ asm__ ("movl% 0,% eax":: "a" (addr))
_ _ asm__ (".byte 0x0f, 0x18, 0x00")
}
Inline unsigned long long GetCPUTickCount ()
{
Unsigned long high42 = 0
Unsigned long low32 = 0
_ _ asm__ ("RDTSC": "= a" (low32), "= d" (high42))
Unsigned long long counter = high42
Counter = (counter