我看到所以这个帖子里面含有C code,以获得最新的CPU周期数:
I saw this post on SO which contains C code to get the latest CPU Cycle count:
CPU用C基于循环计数的分析/ C ++的Linux x86_64的
有没有一种方法,我可以使用用C本code ++(Windows和Linux解决方案的欢迎)?虽然写在C(C是C ++的一个子集),我不是太肯定,如果这个code将在C ++项目工作,如果没有,怎么把它翻译?
Is there a way I can use this code in C++ (windows and linux solutions welcome)? Although written in C (and C being a subset of C++) I am not too certain if this code would work in a C++ project and if not, how to translate it?
我使用的x86-64
I am using x86-64
EDIT2:
发现这个功能,但不能让VS2010承认汇编。我需要包括什么? (我相信我有交换 uint64_t中到长长窗户....?)
Found this function but cannot get VS2010 to recognise the assembler. Do I need to include anything? (I believe I have to swap uint64_t to long long for windows....?)
static inline uint64_t get_cycles() { uint64_t t; __asm volatile ("rdtsc" : "=A"(t)); return t; }EDIT3:
从上面code我得到的错误:
From above code I get the error:
错误C2400:在运code'内联汇编语法错误;找到数据 类型'
"error C2400: inline assembler syntax error in 'opcode'; found 'data type'"
可能有人请帮忙吗?
推荐答案直接拿出我的项目之一:
Pulled directly out of one of my projects:
#include <stdint.h> // Windows #ifdef _WIN32 #include <intrin.h> uint64_t rdtsc(){ return __rdtsc(); } // Linux/GCC #else uint64_t rdtsc(){ unsigned int lo,hi; __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); return ((uint64_t)hi << 32) | lo; } #endif