Re: calculating time taken for an operation in CPU Cycles
- From: "Evenbit" <nbaker2328@xxxxxxxxxxx>
- Date: 9 Jun 2006 14:39:23 -0700
nani wrote:
Hi All,
I want to calculate the no of CPU cycles(nano seconds) consumed for a
particular operation using a c++ code snippet.I have written the
following code snippet to calculate the same.
[Begin]
#if defined(_MSC_VER) || defined(__BORLANDC__)
#define EPOCHFILETIME (116444736000000000i64)
#else
#define EPOCHFILETIME (116444736000000000LL)
#endif
#include <windows.h>
#include <stdio.h>
#include <sys\timeb.h>
#include <stdlib.h>
int add(int a,int b)
{
return (a+b);
}
int main()
{
FILETIME ft;
LARGE_INTEGER li;
__int64 t1,t2;
SYSTEMTIME st1,st2;
int a=0,b=5,c=6;
GetSystemTimeAsFileTime(&ft);
FileTimeToSystemTime(&ft,&st1);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t1 = (li.QuadPart)*100;
t1 = t1- EPOCHFILETIME;
for(unsigned int i =0; i < 6000000;i++)
//a = b+c;
//a = add(b,c);
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t2 = (li.QuadPart)*100;
t2 = t2- EPOCHFILETIME;
FileTimeToSystemTime(&ft,&st2);
printf("time taken to perform operation is %d nanoseconds\n",t2-t1);
printf("time taken to perform operation is %d
milliseconds\n",st2.wMilliseconds - st1.wMilliseconds);
}
[End]
When i run the for loop "for(unsigned int i =0; i < 6000000;i++)" i get
the time consumed to be a greater the "0".
But when i perform "a = b+c" or "a = add(b,c)" i get the time consumed
to be 0.But how can a operation consume no CPU cycle or time (in
nanosecond).
I would like comments and suggestions to improve this snippet to get
the correct Cycle count.
Well, if you have a P2P app running in the background or a bunch of
malware or something else constantly sucking-up your CPU cycles, then
your snippet will fail to report the actual cycles used *only* by your
test code {especially when it consists of more than a few trivial ops}.
I suggest you take a look at either GetProcessTimes or GetThreadTimes.
Nathan.
.
- References:
- Prev by Date: Re: BYTE x87 issues
- Next by Date: Re: Program abnormally terminates
- Previous by thread: calculating time taken for an operation in CPU Cycles
- Next by thread: Re: calculating time taken for an operation in CPU Cycles
- Index(es):
Relevant Pages
|
Loading