Re: calculating time taken for an operation in CPU Cycles




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.

.



Relevant Pages

  • Re: calculating time taken for an operation in CPU Cycles
    ... QueryPerformanceCounter ... #define EPOCHFILETIME ... to be 0.But how can a operation consume no CPU cycle or time (in ...
    (alt.lang.asm)
  • calculating time taken for an operation in CPU Cycles
    ... following code snippet to calculate the same. ... #define EPOCHFILETIME ... to be 0.But how can a operation consume no CPU cycle or time (in ...
    (alt.lang.asm)
  • Re: Where to find statistics for fetched programs?
    ... I got the question how frequently programmes are fetched, ... and then how much CPU they consume. ... This in order to put together kind of a hit parade of big CPU consumers & then tackle their tuning. ... RMF can't help me while Monitor I and Monitor III provide long-term data collection about system workload and resource utilization, while covering all hardware and software components of the system. ...
    (bit.listserv.ibm-main)
  • Where to find statistics for fetched programs?
    ... I got the question how frequently programmes are fetched, ... and then how much CPU they consume. ... This in order to put together kind of a hit parade of big CPU consumers & then tackle their tuning. ... RMF can't help me while Monitor I and Monitor III provide long-term data collection about system workload and resource utilization, while covering all hardware and software components of the system. ...
    (bit.listserv.ibm-main)
  • Re: CPU usage, memory and virtual memory
    ... some applications can consume close to 100% cpu ... usage and freeze up the machine. ... tells me it consumes 99% cpu and a lot of memory and virtual memory ...
    (microsoft.public.vc.mfc)

Loading