3

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.

Romaan
  • 2,645
  • 5
  • 32
  • 63
  • Instruction? In milliseconds? I guess it's an ancient computational machine you're talking about. You can measure it with a stopwatch... – valdo Nov 01 '12 at 06:15
  • Very funny... But thanks – Romaan Nov 01 '12 at 06:16
  • 1
    An alternative is to read the assembly code that your compiler generates (try the -S switch with gcc), then look up the number of clock cycles for each assembly instruction in the documentation for the target processor. – potrzebie Nov 01 '12 at 07:48

3 Answers3

4
#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?

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

Community
  • 1
  • 1
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
3

One instruction will take a lot shorter than 1 millisecond to execute. 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.

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).

idmean
  • 14,540
  • 9
  • 54
  • 83
dave
  • 4,812
  • 4
  • 25
  • 38
  • I need to measure the time taken for execution of set of instructions. But the time taken is shorter than 1 milli second. I have tried using clock function already. Any suggestions??? – Romaan Nov 01 '12 at 06:14
0

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. 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).

Kiith Nabaal
  • 366
  • 2
  • 5
  • 16