我怎样才能找到编程与C CPU频率 [英] How can I programmatically find the CPU frequency with C

查看:170
本文介绍了我怎样才能找到编程与C CPU频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出是否有反正让我的C code是运行系统的CPU频率的想法。

I'm trying to find out if there is anyway to get an idea of the CPU frequency of the system my C code is running on.

要澄清,我正在寻找一个抽象的解决方案,(一个不会被绑定到特定的体系结构或OS),可以给我说,我的code是执行计算机的操作频率的想法上。我并不需要准确的说,但我想是在棒球场(即我有一个2.2GHz的处​​理器,我希望能在我的计划告诉我中了几百那兆赫)

To clarify, I'm looking for an abstract solution, (one that will not be tied to a specific architecture or OS) which can give me an idea of the operating frequency of the computer that my code is executing on. I don't need to be exact, but I'd like to be in the ball park (ie. I have a 2.2GHz processor, I'd like to be able to tell in my program that I'm within a few hundred MHz of that)

有没有人有一个想法,使用标准的C code?

Does anyone have an idea use standard C code?

推荐答案

您如何找到CPU的频率是既架构和操作系统的依赖,并没有抽象的解决方案。

How you find the CPU frequency is both architecture AND OS dependent, and there is no abstract solution.

如果我们超过20年前和你使用的操作系统没有上下文切换和CPU执行,以给它的指示,你可以写在一个循环和时间,一些C code,然后根据装配它被编译成计算的在运行时指令数。这已经使得每个指令需要1个时钟周期的假设,这是一种相当差的假设自从流水线处理器

If we were 20+ years ago and you were using an OS with no context switching and the CPU executed the instructions given it in order, you could write some C code in a loop and time it, then based on the assembly it was compiled into compute the number of instructions at runtime. This is already making the assumption that each instruction takes 1 clock cycle, which is a rather poor assumption ever since pipelined processors.

但是,任何现代操作系统将多个进程之间切换。即使如此,你可以尝试时间循环运行一组相同的的(忽略需要的页面错误和其他多种原因,您的处理器可能停滞时间),并得到一个中间值。

But any modern OS will switch between multiple processes. Even then you can attempt to time a bunch of identical for loop runs (ignoring time needed for page faults and multiple other reasons why your processor might stall) and get a median value.

和即使previous解决方案的工作,你必须多发射处理器。与任何现代的处理器,它是公平的游戏重新整理您的指示,问题一群人在同一个时钟周期,甚至可以跨越他们拆分内核

And even if the previous solution works, you have multi-issue processors. With any modern processor, it's fair game to re-order your instructions, issue a bunch of them in the same clock cycle, or even split them across cores.

这篇关于我怎样才能找到编程与C CPU频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆