 |
|
 |
Sometimes when member struct alignment is set to 1 byte, QueryPerformanceCounter fails. I don't know exactly why, but it's a concern. The call then returns odd values. I discovered this, because i had to set the alignment in VC++ in VS2005 to 1 byte (had to write structs to sockets, sizeof had to be accurate) and a waiting-loop suddenly went to infinity ... Just a heads up.
|
|
|
|
 |
|
 |
I am not sure why the call is failing within your app / on your test computer.
Struct alignment of 1 works fine in 6.0. Perhaps it is a VS2005/1 byte alignment compile problem?
Are you runnning on a dual processor system?
There has been mention of problems with QueryPerformanceCounter on some machines with dual processors - it seems that in a tight loop the value can decrease. There is no checks in CPerfTimer for this.
If you post the wating loop timer part, maybe I can find the problem.
|
|
|
|
 |
|
 |
Hi..
when I compile this code, it looks ok, no error..
but when I try to call the Start(TRUE), it have application error at
inline void CPerfTimer::Start(BOOL bReset)
{ // Start from current value or optionally from 0
__int64 i;
QueryPerformanceCounter((LARGE_INTEGER *)&i);
Lock(); <<<================================== error point at here
if ((!bReset) && (m_Start < 0))
m_Start += i; // We are starting with an accumulated time
else
m_Start = i; // Starting from 0
Unlock();
}
is it anything wrong..??
pls advise..
regards,
shizu (^.^)
|
|
|
|
 |
|
 |
Sorry, you have not given enough info to determine what the problem may be.
You have pasted in some CPerfTimer code, but have not included any code
showing how you are using CPerfTimer.
For example, you should be able use CPerfTimer like this:
#include "PerfTimer.h"
CPerfTimer T;
T.Start(TRUE);
//... do something...
TRACE("Elapsed Seconds: %0.4f\n",T.Elapsed());
//...
The Lock call is simply an empty method in the CPerfTimer class.
If you are using the CPerfTimerT class, then it it uses a mutex (previous
comments have pointed out that a critical section would be faster).
|
|
|
|
 |
|
 |
when i try to compile CPerfTimer class on vc++ express I got the following errors:
Error 7 error C2146: syntax error : missing ';' before identifier 'm_hMutex' c:\...\Timer.h 110
Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\...\Timer.h 110
Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\...\Timer.h 110
Error 10 error C2065: 'm_hMutex' : undeclared identifier c:\...\Timer.h 82
Error 11 error C3861: 'CreateMutex': identifier not found c:\...\Timer.h 82
Error 12 error C3861: 'CreateMutex': identifier not found c:\...\Timer.h 88
Error 13 error C3861: 'CreateMutex': identifier not found c:\...\Timer.h 94
Error 14 error C3861: 'CloseHandle': identifier not found c:\...\Timer.h 99
Error 15 error C3861: 'WaitForSingleObject': identifier not found c:\...\Timer.h 107
Error 16 error C3861: 'ReleaseMutex': identifier not found c:\...\Timer.h 108
Error 17 error C3861: 'QueryPerformanceFrequency': identifier not found c:\...\Timer.h 117
Error 18 error C3861: 'QueryPerformanceCounter': identifier not found c:\...\Timer.h 151
Error 19 error C3861: 'QueryPerformanceCounter': identifier not found c:\...\Timer.h 169
Error 23 fatal error C1071: unexpected end of file found in comment c:\...\Timer.h 400
Error 30 error C2146: syntax error : missing ';' before identifier 'm_hMutex' c:\...\Timer.h 110
Error 31 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\...\Timer.h 110
Error 32 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\...\Timer.h 110
Error 33 error C2065: 'm_hMutex' : undeclared identifier c:\...\Timer.h 82
Error 34 error C3861: 'CreateMutex': identifier not found c:\...\Timer.h 82
Error 35 error C3861: 'CreateMutex': identifier not found c:\...\Timer.h 88
Error 36 error C3861: 'CreateMutex': identifier not found c:\...\Timer.h 94
Error 37 error C3861: 'CloseHandle': identifier not found c:\...\Timer.h 99
Error 38 error C3861: 'WaitForSingleObject': identifier not found c:\...\Timer.h 107
Error 39 error C3861: 'ReleaseMutex': identifier not found c:\...\Timer.h 108
Error 40 error C3861: 'QueryPerformanceFrequency': identifier not found c:\...\Timer.h 117
Error 41 error C3861: 'QueryPerformanceCounter': identifier not found c:\...\Timer.h 151
Error 42 error C3861: 'QueryPerformanceCounter': identifier not found c:\...\Timer.h 169
Error 46 fatal error C1071: unexpected end of file found in comment c:\...\Timer.h 400
any help is appriciated.
|
|
|
|
 |
|
 |
I do not know about vc++ express. And, I have no idea what kind of project etc. you are trying to compile into. The code does not compile "stand alone" because it uses Win32 calls. You need to include the files in a Win32 project for an application. It looks like the compiler is not compiling for a Win32 app because it does not recognize the Win32 calls (windows.h or others according to project type).
Also, please see the "possible bug" message for a bug fix to a - operator.
|
|
|
|
 |
|
 |
Just wrap the empty strings in CreateMutex "" like this _T("") and it will work fine.
|
|
|
|
 |
|
 |
Hi Mr Wyant,
I’ve been using your CPerfTimer class for some-time now for home projects. Whilst undertaking an inspection activity at work, I noticed that: CPerfTimer::operator-(const double Secs) const calculates the result by employing the +=(double) operator, rather than the -=(double) operator.
This is my test-case:
void test_timer_math_operator6()
{
//testing CPerfTimer CPerfTimer::operator-(const double Secs) const
CPerfTimer TestTime1(true);
Sleep(10);//wait a time
TestTime1.Stop();
CPerfTimer TestTime2(true);
Sleep(30);//wait a longer time
TestTime2.Stop();
CPerfTimer TestTimeResult(false);//create a temporary timer object to hold the result
TestTimeResult = TestTime2 - TestTime1.Elapsed();//TEST SUBJECT
assert( DOUBLE_EQ( TestTimeResult.Elapsed() , (TestTime2.Elapsed() - TestTime1.Elapsed() ) ) );
}
I believe that the operator should be thus:
inline CPerfTimer CPerfTimer::operator-(const double Secs) const
{
CPerfTimer Result(*this);
Result -= Secs; //was +=
return Result;
}
Its late, so forgive me if my eyes have deceived me!
Regards
Tim
|
|
|
|
 |
|
 |
Looking at the code, it seems that you are correct.
Changing the += to -= is the correct fix. I guess I never used that operator.
Thanks.
|
|
|
|
 |
|
 |
I'm using CPerfTimer for a long time. It works great.
Few days ago I tried it on a laptop with Windows XP.
I wrote:
Timer.Start (TRUE);
while (1)
{
Timetag=Timer.Elapsed ()
Sleep (40)
}
The value of Timetag was set to 0 every few seconds and then grew up.
The same code worked great on a PC with Windows 2000.
The value of Timetag was not set to 0.
Can you help ?
|
|
|
|
 |
|
 |
The problem must be related to the actual code you are using. If there is a general problem with running on XP, then I am very interested in getting the complete code to a program that exhibits the problem. The code include in this message runs fine (with syntax changes) on XP. I have emailed you.
|
|
|
|
 |
|
 |
A minor change (very easy) to get this to work under Windows CE is to change the mutex creation to using the _T macro: eg:
CreateMutex(NULL,FALSE,_T(""));
Once you change it, it works great!
Thanks!
|
|
|
|
 |
|
|
 |
|
 |
Thanks for this class. I had some problems with the granularity of standard system timers and this
worked out quite nicely. This saved me a lotof time in making something similar myself.
A couple of questions though.
What is the necessity of the m_Adjust member? It never seems to get set to anything other than 0.
Also, I am using seperate instances of CPerfTimer in different threads. I am assuming that I do not
need to use the CPerfTimerT since I am not using the same instance of the object in each thread.
Is my assumption correct?
|
|
|
|
 |
|
 |
I know this reply is very late...
The m_Adjust does get a value on slower processors. Perhaps your processor can run the code in less than a microsecond. m_Adjust adjusts for the time the code takes. This allows the timer to get close to microsecond accuracy. Without it, the accuracy would suffer on slower processors.
The only time you might want to use CPerfTimerT is if you are using the same instance (variable) in multiple threads. Since you are not sharing an instance of CPerfTimer between threads, CPerfTimerT is not needed. I only included CPerfTimerT it because someone asked for a thread safe version. I have never had a need for it.
|
|
|
|
 |
|
 |
Why not use a critical section instead of a mutex in your CPerfTimeT class? A mutex is a kernel object and will consume a lot more performance/time then a critical section.
|
|
|
|
 |
|
 |
That would be fine. It is easy to change to use a critical section.
However, the performance counter functions themselves are fairly slow. If performance is an issue, it would probably be a good idea to use some other method of timing. I wrote this class to simply wrap the functions in a class that could provide accurate timing, mimimal memory use, and ease of use. At the time, I did not think to use a critical section because I thought a mutex was fast enough. There is info on the web about high performance methods of timing that would probably be a better choice for programs that will call into the timer code a lot in a short period of time. IOW, programs that need the timer calls to be very fast. This classes timer calls themselves are not very fast.
|
|
|
|
 |
|
 |
I work on a project under VC++ in german Edition.I wanted to include and use your timer to check out my performance.But it doesn't work.
There is an error about it,so I can't compile it.Do I have to convert anything I the code or do you have an other idea,why it does not work?
|
|
|
|
 |
|
 |
I cannot even guess what is wrong without knowing what error you are getting.
|
|
|
|
 |
|
 |
Now I found my mistake.After including StdAfx.h it works.Without this there where about 50 mistakes which show very different parts of code.But now I can use your timer.
|
|
|
|
 |
|
 |
That posting was too early.I still get these errors.Here I show you them:
c:\eigene projekte\fft\fft_1\perftimer.h(22) : error C2629: 'class CPerfTimer (' unerwartet
c:\eigene projekte\fft\fft_1\perftimer.h(22) : error C2334: Unerwartete(s) Token vor '{'; sichtbarer Funktionsrumpf wird übersprungen
c:\eigene projekte\fft\fft_1\perftimer.h(28) : error C2061: Syntaxfehler : Bezeichner 'BOOL'
c:\eigene projekte\fft\fft_1\perftimer.h(31) : error C2146: Syntaxfehler : Fehlendes ';' vor Bezeichner 'IsRunning'
c:\eigene projekte\fft\fft_1\perftimer.h(31) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(33) : error C2146: Syntaxfehler : Fehlendes ';' vor Bezeichner 'IsSupported'
c:\eigene projekte\fft\fft_1\perftimer.h(33) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(58) : error C2143: Syntaxfehler : Fehlendes ';' vor '<'
c:\eigene projekte\fft\fft_1\perftimer.h(58) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(59) : error C2143: Syntaxfehler : Fehlendes ';' vor '>'
c:\eigene projekte\fft\fft_1\perftimer.h(59) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(60) : error C2143: Syntaxfehler : Fehlendes ';' vor '<='
c:\eigene projekte\fft\fft_1\perftimer.h(60) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(61) : error C2143: Syntaxfehler : Fehlendes ';' vor '>='
c:\eigene projekte\fft\fft_1\perftimer.h(61) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(63) : error C2143: Syntaxfehler : Fehlendes ';' vor '<'
c:\eigene projekte\fft\fft_1\perftimer.h(63) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(64) : error C2143: Syntaxfehler : Fehlendes ';' vor '>'
c:\eigene projekte\fft\fft_1\perftimer.h(64) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(65) : error C2143: Syntaxfehler : Fehlendes ';' vor '<='
c:\eigene projekte\fft\fft_1\perftimer.h(65) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(66) : error C2143: Syntaxfehler : Fehlendes ';' vor '>='
c:\eigene projekte\fft\fft_1\perftimer.h(66) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(71) : error C2061: Syntaxfehler : Bezeichner 'BOOL'
c:\eigene projekte\fft\fft_1\perftimer.h(83) : error C2629: 'class CPerfTimerT (' unerwartet
c:\eigene projekte\fft\fft_1\perftimer.h(83) : error C2334: Unerwartete(s) Token vor '{'; sichtbarer Funktionsrumpf wird übersprungen
c:\eigene projekte\fft\fft_1\perftimer.h(113) : error C2146: Syntaxfehler : Fehlendes ';' vor Bezeichner 'm_hMutex'
c:\eigene projekte\fft\fft_1\perftimer.h(113) : error C2501: 'HANDLE' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(113) : error C2501: 'm_hMutex' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(116) : error C2065: 'BOOL' : nichtdeklarierter Bezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(116) : error C2146: Syntaxfehler : Fehlendes ')' vor Bezeichner 'bStart'
c:\eigene projekte\fft\fft_1\perftimer.h(116) : error C2182: 'Init' : Ungueltige Verwendung des Typs 'void'
c:\eigene projekte\fft\fft_1\perftimer.h(116) : error C2433: 'Init' : 'inline' bei der Deklaration von Daten nicht zulaessig
c:\eigene projekte\fft\fft_1\perftimer.h(116) : error C2350: 'CPerfTimer::Init' ist kein statisches Element
c:\eigene projekte\fft\fft_1\perftimer.h(116) : error C2059: Syntaxfehler : ')'
c:\eigene projekte\fft\fft_1\perftimer.h(117) : error C2143: Syntaxfehler : Fehlendes ';' vor '{'
c:\eigene projekte\fft\fft_1\perftimer.h(117) : error C2447: Funktionskopf fehlt - Parameterliste im alten Stil?
c:\eigene projekte\fft\fft_1\perftimer.h(151) : error C2146: Syntaxfehler : Fehlendes ')' vor Bezeichner 'bReset'
c:\eigene projekte\fft\fft_1\perftimer.h(151) : error C2182: 'Start' : Ungueltige Verwendung des Typs 'void'
c:\eigene projekte\fft\fft_1\perftimer.h(151) : error C2433: 'Start' : 'inline' bei der Deklaration von Daten nicht zulaessig
c:\eigene projekte\fft\fft_1\perftimer.h(151) : error C2350: 'CPerfTimer::Start' ist kein statisches Element
c:\eigene projekte\fft\fft_1\perftimer.h(151) : error C2059: Syntaxfehler : ')'
c:\eigene projekte\fft\fft_1\perftimer.h(152) : error C2143: Syntaxfehler : Fehlendes ';' vor '{'
c:\eigene projekte\fft\fft_1\perftimer.h(152) : error C2447: Funktionskopf fehlt - Parameterliste im alten Stil?
c:\eigene projekte\fft\fft_1\perftimer.h(172) : error C2065: 'QueryPerformanceCounter' : nichtdeklarierter Bezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(172) : error C2065: 'LARGE_INTEGER' : nichtdeklarierter Bezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(172) : error C2059: Syntaxfehler : ')'
c:\eigene projekte\fft\fft_1\perftimer.h(181) : error C2143: Syntaxfehler : Fehlendes ';' vor 'tag::id'
c:\eigene projekte\fft\fft_1\perftimer.h(181) : error C2433: 'BOOL' : 'inline' bei der Deklaration von Daten nicht zulaessig
c:\eigene projekte\fft\fft_1\perftimer.h(181) : error C2501: 'BOOL' : Fehlende Speicherklasse oder Typbezeichner
c:\eigene projekte\fft\fft_1\perftimer.h(181) : fatal error C1004: Unerwartetes Dateiende gefunden
Sorry but they are all in German but of course you can see it at the errornumber.
|
|
|
|
 |
|
 |
Same error happened to me just include windows.h to PerfTimer.h. Result:
#ifndef __PERFTIMER_H__
#define __PERFTIMER_H__
#include <windows.h> // this one
class CPerfTimer
|
|
|
|
 |
|
 |
Hello,
I'm haveing trouble compiling the class, it keeps saying stdafx.h is missing. I've tried including mfc as shared or static, I tried including it directly in the project stil no luck. Could the other files in my project being .C instead of .cpp cause the problem? I'm using MSVC 6.0 btw.
Thanks in advance,
Steve
|
|
|
|
 |
|
 |
stdafx.h is included in cpp files for precompiled headers. It only contains global includes for MFC or whatever you want. If you are using precompiled headers in your project, you should have a stdafx.h and stdafx.cpp. If you do not and your project compiles fine without including the timer files, then simply remove or remark out the #include line.
|
|
|
|
 |
|
 |
Hello,
I'm haveing trouble compiling the class, it keeps saying stdafx.h is missing. I've tried including mfc as shared or static, I tried including it directly in the project stil no luck. Could the other files in my project being .C instead of .cpp cause the problem?
Thanks in advance,
Steve
|
|
|
|
 |