Solved

Computing Program execution time?

Posted on 2001-08-28
17
489 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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