Function Timing Simple Question 
Author Message
 Function Timing Simple Question

Quote:
>Can anyone please provide some code as to how I would stopwatch a call to a
>function in milliseconds in MFC?

For the highest resolution, use QueryPerformanceCounter &
QueryPerformanceFrequency:

        LARGE_INTEGER pc1, pc2, freq;

        QueryPerformanceFrequency( &freq );
        QueryPerformanceCounter( &pc1 );

        ...

        QueryPerformanceCounter( &pc2 );
        pc2.QuadPart -= pc1.QuadPart;

        pc2.QuadPart = pc2.QuadPart*1000 / freq.QuadPart;

        printf("Time taken %I64d mS\n", pc2.QuadPart );

Dave
--
MVP VC++ FAQ: http://www.*-*-*.com/
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Fri, 05 Nov 2004 19:53:37 GMT  
 Function Timing Simple Question
Try
QueryPerformanceCounter(...) and
QueryPerformanceFrequency(...)

Cheers
Bjarne Nielsen



Quote:
> Hi,

> Can anyone please provide some code as to how I would stopwatch a call to
a
> function in milliseconds in MFC?

> For example

> Init Timer
> Call Function
> End Timer
> MessageBox(Time Elapsed)



Fri, 05 Nov 2004 19:00:34 GMT  
 Function Timing Simple Question
Not only are the responses about QueryPerformanceCounter correct, they are the *only* way
to get the information you want.
                        joe

Quote:

>Hi,

>Can anyone please provide some code as to how I would stopwatch a call to a
>function in milliseconds in MFC?

>For example

>Init Timer
>Call Function
>End Timer
>MessageBox(Time Elapsed)

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Sat, 06 Nov 2004 01:43:12 GMT  
 Function Timing Simple Question
Joe,

I've been using calls to _ftime to fill _timeb structures before and
after the code in question and then comparing the structures to get
the elapsed time (in milliseconds). This *seems* to work OK. Is there
a problem with _ftime?

Bert  


Quote:
>Not only are the responses about QueryPerformanceCounter correct, they are the *only* way
>to get the information you want.
>                    joe


>>Hi,

>>Can anyone please provide some code as to how I would stopwatch a call to a
>>function in milliseconds in MFC?

>>For example

>>Init Timer
>>Call Function
>>End Timer
>>MessageBox(Time Elapsed)

>Joseph M. Newcomer [MVP]

>Web: http://www3.pgh.net/~newcomer
>MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm



Sat, 06 Nov 2004 07:48:39 GMT  
 Function Timing Simple Question

Quote:

> Joe,

> I've been using calls to _ftime to fill _timeb structures before and
> after the code in question and then comparing the structures to get
> the elapsed time (in milliseconds). This *seems* to work OK. Is there
> a problem with _ftime?

> Bert

The problem is crude time resolution.  All time APIs work from the
system clock, which is only updated every few 10s of milliseconds under
Windows.  QueryPerformanceCounter is updated by hardware about 10,000
times more often than the Windows system clock.

--
Scott McPhillips [VC++ MVP]



Sat, 06 Nov 2004 09:43:45 GMT  
 Function Timing Simple Question

Quote:


>> Joe,

>> I've been using calls to _ftime to fill _timeb structures before and
>> after the code in question and then comparing the structures to get
>> the elapsed time (in milliseconds). This *seems* to work OK. Is there
>> a problem with _ftime?

>> Bert

>The problem is crude time resolution.  All time APIs work from the
>system clock, which is only updated every few 10s of milliseconds under
>Windows.  QueryPerformanceCounter is updated by hardware about 10,000
>times more often than the Windows system clock.

So even though  _ftime ostensibly provides time in milliseconds, it's
resolution is actually an order of magnitude or two less than that,
eh? That's a good thing to know! Thanks.


Sat, 06 Nov 2004 10:10:39 GMT  
 Function Timing Simple Question
A value in milliseconds does not mean the timer is running at the millisecond level. For
example, NT has timer resolution of 10ms (uniprocessor) or 15 ms (multiprocessor), and the
MS-DOS systems have timer resolution of 55ms. So the difference between two readings will
either be 0 or be a multiple of the timer resolution, which is far too coarse for most
high-performance timings. Never be misled by confusing the published resolution with the
practical resolution (for example, there are time-related APIs that use 64-bit values in
100ns units, but there is no guarantee that any system in the forseeable future will give
a count interval of 1 between two calls, or cause your thread to be scheduled to an
accuracy of 100ns). For example, I took the example under _ftime, fixed the bug in in (use
%03u instead of %hu), and added a test to print out only when the millitm field changes,
and I get results like

The time is Tue May 21 02:03:21.714 2002
The time is Tue May 21 02:03.21.724 2002
The time is Tue May 21 02:03.21.734 2002
...
The time is Tue May 21 02:03:22.094 2002
The time is Tue May 21 02:03:22.105 2002
The time is Tue May 21 02:03:22.115 2002
                                joe

Quote:

>Joe,

>I've been using calls to _ftime to fill _timeb structures before and
>after the code in question and then comparing the structures to get
>the elapsed time (in milliseconds). This *seems* to work OK. Is there
>a problem with _ftime?

>Bert  


>>Not only are the responses about QueryPerformanceCounter correct, they are the *only* way
>>to get the information you want.
>>                        joe


>>>Hi,

>>>Can anyone please provide some code as to how I would stopwatch a call to a
>>>function in milliseconds in MFC?

>>>For example

>>>Init Timer
>>>Call Function
>>>End Timer
>>>MessageBox(Time Elapsed)

>>Joseph M. Newcomer [MVP]

>>Web: http://www3.pgh.net/~newcomer
>>MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Sat, 06 Nov 2004 14:08:03 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. simple time(NULL) question

2. Simple Time Question

3. A very simple question about TIME

4. Simple C question about time elapse..

5. "should be simple" date/time question

6. Simple Date/Time stamp question

7. simple malloc question in functions

8. Simple question about function return

9. Simple question: Member function templates in template classes under VC++ 5.0

10. Simple question about Pointers to Functions

11. Function question Part 2 ( RUN TIME ERROR PROBLEM )

12. time function question

 

 
Powered by phpBB® Forum Software