Monday, May 24, 2010

C program question on processor timing.....?

Say I have a functions called Calculate_sum(), I want to know exactly how many processor cycles it takes to caclucalte that. I am using visual c++ studio compiler on console application, non win32. I tried to use the clock() function but the resolution of the time isn't that much high. Seems like the function has to take several microseconds to get a valuable value.





so what else can I use, say for instant my function takes only micro seconds to complete its task. And I am more interested in processor cycles and not in the time.


like if I can get a result that tells me, say, 2000 cycles or something of that nature.


thanks.

C program question on processor timing.....?
That's actually a very difficult question. First, the correlation between processor cycles and instructions can vary due to processor pipelining. Second, standard C does not have any routines to determine processor timing.





If you can call platform specific functions you may be able to get more precise time information. I'm a Windows programmer, so on that platform you have:


QueryPerformanceCounter()


This function gives generally microsecond precision times. The specific precision is given by QueryPerformanceFrequency.





*edited to add *


If you are running on an Intel chip, and you can add assembly code to your program, look up the following instruction:


RDTSC—Read Time-Stamp Counter


This counter is incremented once every clock cycle.
Reply:You should know that every programming language spot the time under 0.001 second to zero
Reply:I usually do time type things on Unix/Linux, so I can't guarantee it is the same. I would typically use function from the shell like date() which can return in nanoseconds. Perhaps trying doing some searches on the C library for date or time. They may have options for converting to cycles, but should at least have options for nanoseconds.
Reply:I don't really program that much on windows so I am not completely sure what tools are available to you especially for free, but what it looks like you want is a basic CPU profiler.





There are lots and lots of them out there, you just need to pick one that works. Alternately you could see what the Windows equivalent API is for the POSIX times API from sys/times.h.


No comments:

Post a Comment