Solved

Computing Program execution time?

Posted on 2001-08-28
17
491 Views
Last Modified: 2008-02-26
How to compute execution time/Clock cycles in VC++ editor  for c or c++ programs?

Suggestion and hints on the same at the earliest would be helpful.

Sateesh Mandre.
0
Comment
Question by:SateeshMandre
  • 3
  • 2
  • 2
  • +8
17 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6435244
CPU usage: how find cpu usage for all (or some) process:
http://www.edm2.com/0607/cpu.html
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6435488
You can use the pseudo watch variable #clk to time the execution of the program between each trace. It's not as accurate or robust as the professional tools that do it, but it's usefull.

Define two watch variables:
@clk
and
@clk = 0

then, whenever you trace (hit f10, or f11, or stop at a break point) you'll see the time that's passed since the last trace in this watch. It's shows the time in microseconds i think.
0
 

Author Comment

by:SateeshMandre
ID: 6435522
Thanks for the quick suggestion, but i have already tried this solution earlier, but i am getting the minimum response of 10 milliseconds,

i.e i started the timer at the begining of the program and then checked it before the exit of the program which is giving me a result of 10 milliseconds, and when i put the code in a loop and executing it, i am getting values multiples of 0 milliseconds. The problem is, when i want to check the execution time of a single function, i checked the timediff after each function call, but am  getting 0 milliseconds.   So I want to know if there is any method, where i can check the execution time of the function with some code or tools.

Sateesh Mandre
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6435544
Are you saying that you've stepped over the function and it showed you 0 microseconds? That's hard to believe.
0
 
LVL 1

Expert Comment

by:jacobkristensen
ID: 6435739
When I want to time some function, I usually put int the line:

long Start = timeGetTime();

In the beginning, and the line:

long Elapsed = timeGetTime() - Start;

in the end. Elapsed then represents the execution time...
0
 

Author Comment

by:SateeshMandre
ID: 6435778
Thanks for your suggestion, but i have already done this This is similer to previous suggestion I got.I want in terms of micro seconds.
Sateesh Mandre
0
 
LVL 1

Expert Comment

by:jacobkristensen
ID: 6435798
Have you tried a high resolution timer?
fx by calling QueryPerformanceFrequency()
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6435854
>>So I want to know if there is any method, where i can check the execution time of the function with
some code or tools
If you want some tool, try
TakInfo2000 from
www.iarsn.com
Frim this tool you know cpu,memory, priority,files ... of
every apps in system.
As I know, he(Igor Arsenin) uses methods ~ same as in my comment.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:qbawler311
ID: 6436900
Windows can only measure to 10 milliseconds.  So if your function only took 4 milliseconds to do a loop.  It will read 0 milliseconds.  It's impossible to measure in microseconds, Windows isn't that accurate.
0
 
LVL 5

Accepted Solution

by:
BlackDiamond earned 25 total points
ID: 6437970
0
 
LVL 2

Expert Comment

by:bkdc
ID: 6438425
I agree with qbawler311
M$ stuff:
"
The GetTickCount() API should not be used for resolution critical
algorythms. Instead, QueryPerformanceCounter() and
QueryPerformanceFrequency() should be used.

The Win32 API QueryPerformanceCounter() returns the resolution of a
high- resolution performance counter if the hardware supports one. For
x86, the resolution is about 0.8 microseconds (0.0008 ms). You need to
call QueryPerformanceFrequency() to get the frequency of the
high-resolution performance counter. "


void stuff()
{
  LARGE_INTEGER q,w,freq,overhead,sp,speed;
  if(QueryPerformanceCounter(q)) //long live 386 -won't work (in case you care)
  {
    QueryPerformanceCounter(q);
    QueryPerformanceCounter(w);
    overhead=w-q; //time taken by calls to the 2 funcs.
    QueryPerformanceCounter(q); //start ticking
       //your stuff comes here
    ...........
    //end of the routine to be clocked
    QueryPerformanceCounter(w); //no more ticking
    QueryPerformanceFrequency(freq);
    sp=w-q-overhead; //that's the speed in ticks
       speed=sp/freq;
    cout<<"Time taken:"<<speed;
   }
}
 
//some tweaking might be required, I don't have a Windows machine right now so... not tested (!!! hopefully won't harm your PC, pet or wife :) )
0
 
LVL 2

Expert Comment

by:bkdc
ID: 6438432
>>It's impossible to measure in microseconds, Windows isn't that accurate.

Err..no quite right. It's true that the timer resolution is somewhere around 10ms (that's true on NT/2K, it's slightly different under 9x >=20ms, if you're "lucky" 50ms)
0
 
LVL 2

Expert Comment

by:bkdc
ID: 6438448
SateeshMandre, one suggestion if I may...
Check out msdn.microsoft.com
Not a piece of art but the Knowledge Base it's worth every penny (and it won't cost you points nor time wasted while waiting for an answer from somebody)
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6442147
On NT/2000 try GetProcessTimes api function
0
 
LVL 3

Expert Comment

by:jcgd
ID: 6447009
Maybe this help you:
-------
GetSystemTimeAdjustment(&savedwTimeAdjustment,  &dwTimeIncrement,   &bTimeAdjustmentDisabled);
GetSystemTime(  lpSystemTime );
dwTimeIncrement *= 100;
SetSystemTimeAdjustment(dwTimeIncrement ,FALSE);
func();    
SetSystemTimeAdjustment(savedwTimeAdjustment,TRUE);
GetSystemTime(  lpSystemTime2);
-------
SetSystemTimeAdjustment (first) add dwTimeIncrement for each clock interrupt to time-of-day.

0
 
LVL 11

Expert Comment

by:griessh
ID: 6862515
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "jbawler311, BlackDiamond" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6886409
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange

qbawler311: points for you at http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20279971
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now