获得CPU周期数?

编程入门 行业动态 更新时间:2024-10-23 23:25:34
本文介绍了获得CPU周期数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我看到所以这个帖子里面含有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

更多推荐

获得CPU周期数?

本文发布于:2023-11-30 15:00:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:周期   CPU

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!