timing performance of code

R

ronny

Hi

I am looking to time the performance of my code, I am currently using the
system time "DateTime::Now" however I am finding the results are not very
accurate.

I have been told that I can use the system diagnostics to use a performance
counter. However I have not been able to get any code that relates to
C++.net (managed).

Can anyone help me get started?
 
C

Carl Daniel [VC++ MVP]

ronny said:
Hi

I am looking to time the performance of my code, I am currently using
the system time "DateTime::Now" however I am finding the results are
not very accurate.

I have been told that I can use the system diagnostics to use a
performance counter. However I have not been able to get any code
that relates to C++.net (managed).

Can anyone help me get started?

You can just use the native
QueryPerformanceCounter/QueryPerformanceFrequency Win32 functions from
managed C++. If you'd rather not #include <windows.h>, you could just
declare them:

#pragma unmanaged

extern "C"
{
int __stdcall QueryPerformanceCounter(__int64* p);
int __stdcall QueryPerformanceFrequency(__int64* p);
}

#pragma managed

To use them, call QueryPerformanceCounter at the start and end of your code
block. Call QueryPerformanceFrequency once to determine the resolution of
the counts.

__int64 start;
__int64 end;
::QueryPerformanceCounter(&start);
//
// Your code to be measured here
//
::QueryPerformanceCounter(&end);

__int64 freq;
::QueryPerformanceFrequency(&freq);
double elapsedSeconds = double(end-start)/freq;

HTH

-cd
 
R

ronny

Thanks for your reply Carl. I have tried to add the code you suggested but I
get the following errors


....Include\WinBase.h(9839): error C2733: second C linkage of overloaded
function 'QueryPerformanceCounter' not allowed
....Include\WinBase.h(9846): error C2733: second C linkage of overloaded
function 'QueryPerformanceFrequency' not allowed
....WinBase.h(9844) : see declaration of 'QueryPerformanceFrequency'


I'm not sure if I have added the code to the correct place. My "Form1.h"
looks like this.

***********************************************
#pragma unmanaged
extern "C"
{
int __stdcall QueryPerformanceCounter(__int64* p);
int __stdcall QueryPerformanceFrequency(__int64* p);
}

#pragma managed
#include <stdio.h>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
.......
**********************************************

Ronny
 
C

Carl Daniel [VC++ MVP]

ronny said:
Thanks for your reply Carl. I have tried to add the code you
suggested but I get the following errors


...Include\WinBase.h(9839): error C2733: second C linkage of
overloaded function 'QueryPerformanceCounter' not allowed
...Include\WinBase.h(9846): error C2733: second C linkage of
overloaded function 'QueryPerformanceFrequency' not allowed
...WinBase.h(9844) : see declaration of 'QueryPerformanceFrequency'

Just get rid of the re-definition of the functions - you've obviously
already included <windows.h>.

-cd
 
C

Carl Daniel [VC++ MVP]

Carl said:
Just get rid of the re-definition of the functions - you've obviously
already included <windows.h>.

.... and you'll have to use LARGE_INTEGER (look it up) instead of __int64.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top