|
-
January 27th, 2011, 03:36 AM
#1
C++ Tick timer
Hello,
I need to measure the time taken between 2 functions being called...
startTimer();
endTimer();
that's basically it, i have no Real idea where to start. ;D please help Me, thanks.
-
January 27th, 2011, 04:15 AM
#2
Re: C++ Tick timer
Have a look at GetTickCount example.
Victor Nijegorodov
-
January 27th, 2011, 04:36 AM
#3
Re: C++ Tick timer
DWORD GetTickCount(void);
float FloatCalc()
{
return A+B;
}
void startTimer()
{
tstart = GetTickCount();
}
void endTimer()
{
tend = GetTickCount();
TimeElapsed = (tend - tstart) / 1000;
}
1>------ Build started: Project: Calculation, Configuration: Debug Win32 ------
1> main.cpp
1>e:\programming\c++ projects\c++\calculation\calculation\main.cpp(9): warning C4305: 'initializing' : truncation from 'double' to 'const float'
1>e:\programming\c++ projects\c++\calculation\calculation\main.cpp(10): warning C4305: 'initializing' : truncation from 'double' to 'const float'
1>e:\programming\c++ projects\c++\calculation\calculation\main.cpp(20): error C2373: 'GetTickCount' : redefinition; different type modifiers
1> c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winbase.h(5651) : see declaration of 'GetTickCount'
1>e:\programming\c++ projects\c++\calculation\calculation\main.cpp(30): error C2440: '=' : cannot convert from 'DWORD (__stdcall *)(void)' to 'long'
1>e:\programming\c++ projects\c++\calculation\calculation\main.cpp(30): error C2440: '=' : cannot convert from 'DWORD (__stdcall *)(void)' to 'long'
1> There is no context in which this conversion is possible
1>e:\programming\c++ projects\c++\calculation\calculation\main.cpp(30): error C3861: 'GetTickCount': identifier not found
1>e:\programming\c++ projects\c++\calculation\calculation\main.cpp(34): error C2440: '=' : cannot convert from 'DWORD (__stdcall *)(void)' to 'long'
1>e:\programming\c++ projects\c++\calculation\calculation\main.cpp(34): error C2440: '=' : cannot convert from 'DWORD (__stdcall *)(void)' to 'long'
1> There is no context in which this conversion is possible
1>e:\programming\c++ projects\c++\calculation\calculation\main.cpp(34): error C3861: 'GetTickCount': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
-
January 27th, 2011, 04:55 AM
#4
Re: C++ Tick timer
First of all, you don't need to declare GetTickCount function in your own program.
It's already declared in <winbase.h>
-
January 27th, 2011, 04:28 PM
#5
Re: C++ Tick timer
Since you are using Windows, you can use the performance counter.
Code:
#include <windows.h>
#include <iostream>
unsigned long long GetPerformanceTicks();
unsigned long long GetPerformanceTicksInSecond();
double GetTickSeconds(unsigned long long nTicks,unsigned long long nFreq);
unsigned long long GetTickMilliseconds(unsigned long long nTicks,unsigned long long nFreq);
unsigned long long GetTickMicroseconds(unsigned long long nTicks,unsigned long long nFreq);
int main(int argc,char * argv)
{
unsigned long long nFreq = GetPerformanceTicksInSecond();
unsigned long long nBefore = GetPerformanceTicks();
CallSomeFunction();
unsigned long long nAfter = GetPerformanceTicks();
const unsigned long long nDiff = nAfter - nBefore;
const unsigned long long nMicroseconds = GetTickMicroseconds(nTicks,nFreq);
std::cout << "CallSomeFunction() took" << nMicroseconds << std::endl;
return 0;
}
unsigned long long GetPerformanceTicks()
{
LARGE_INTEGER nValue;
::QueryPerformanceCounter(&nValue);
return nValue.QuadPart;
}
unsigned long long GetPerformanceTicksInSecond()
{
LARGE_INTEGER nFreq;
::QueryPerformanceFrequency(&nFreq);
return nFreq.QuadPart;
}
double GetTickSeconds(unsigned long long nTicks,unsigned long long nFreq)
{
return static_cast<double>(nTicks) / static_cast<double>(nFreq);
}
unsigned long long GetTickMilliseconds(unsigned long long nTicks,unsigned long long nFreq)
{
unsigned long long nTicksInMillisecond = nFreq / 1000;
return nTicks / nTicksInMillisecond;
}
unsigned long long GetTickMicroseconds(unsigned long long nTicks,unsigned long long nFreq)
{
unsigned long long nTicksInMicrosecond = nFreq / 1000000;
return nTicks / nTicksInMicrosecond;
}
Last edited by CppCoder2010; January 28th, 2011 at 03:48 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|