简体   繁体   English

C程序测量指令的执行时间

[英]C Program measure execution time for an instruction

I need to find the time taken to execute a single instruction or a few couple of instructions and print it out in terms of milli seconds. 我需要找到执行单个指令或几条指令所花费的时间,并以毫秒为单位打印出来。 Can some one please share the small code snippet for this. 有人可以分享这个小代码片段。

Thanks.. I need to use this measure the time taken to execute some instructions in my project. 谢谢..我需要使用这个度量来执行项目中的一些指令所花费的时间。

#include<time.h> 
main()
{
clock_t t1=clock();
printf("Dummy Statement\n");
clock_t t2=clock();
printf("The time taken is.. %g ", (t2-t1));

Please look at the below liks too. 请看下面的说法。 What's the correct way to use printf to print a clock_t? 使用printf打印clock_t的正确方法是什么?

http://www.velocityreviews.com/forums/t454464-c-get-time-in-milliseconds.html http://www.velocityreviews.com/forums/t454464-c-get-time-in-milliseconds.html

One instruction will take a lot shorter than 1 millisecond to execute. 执行一条指令将花费不到1毫秒的时间。 And if you are trying to measure more than one instruction it will get complicated (what about the loop that calls the instruction multiple times). 如果你试图测量多个指令,它将变得复杂(如何多次调用指令的循环)。

Also, most timing functions that you can use are just that: functions . 此外,您可以使用的大多数计时功能只是: 功能 That means they will execute instructions also. 这意味着他们也会执行指令。 If you want to time one instruction then the best bet is to look up the specifications of the processor that you are using and see how many cycles it takes. 如果您想为一条指令计时,那么最好的办法是查找您正在使用的处理器的规格,并查看它需要多少个周期。

Doing this programatically isn't possible. 以编程方式执行此操作是不可能的。

Edit: 编辑:

Since you've updated your question to now refer to some instructions. 由于您已将问题更新为现在,请参阅一些说明。 You can measure sub-millisecond time on some processors. 您可以在某些处理器上测量亚毫秒时间。 It would be nice to know the environment. 了解环境会很高兴。 This will work on x86 and linux, other environments will be different. 这将适用于x86和linux,其他环境将有所不同。

Clock get time allows forr sub-nanosecond accuracy. 时钟获取时间允许亚纳秒精度。 Or you can call the rdstc instruction yourself (good luck with this on a multiprocessor or smp system - you could be measuring the wrong thing, eg by having the instruction run on different processors). 或者你可以自己调用rdstc指令(在多处理器或smp系统上运气好 - 你可能正在测量错误的东西,例如让指令在不同的处理器上运行)。

The time to actually complete an instruction depends on the clock cycle time, and the depth of the pipeline the instruction traverses through the processor. 实际完成指令的时间取决于时钟周期时间,以及指令遍历处理器的流水线深度。 As dave said, you can't really find this out by making a program. 正如戴夫所说,通过制作一个程序,你无法真正找到它。 You can use some kind of timing function provided to you by your OS to measure the cpu time it takes to complete some small set of instructions. 您可以使用操作系统为您提供的某种计时功能来测量完成一些小指令所需的CPU时间。 If you do this, try not to use any kind of instructions that rely on memory, or branching. 如果这样做,请尽量不要使用任何依赖内存或分支的指令。 Ideally you might do some kind of logical or arithmetic operations (so perhaps using some inline assembly in C). 理想情况下,您可能会执行某种逻辑或算术运算(因此可能在C中使用某些内联汇编)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM