CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: C++ Tick timer

  1. #1
    Join Date
    Jan 2011
    Posts
    2

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: C++ Tick timer

    Have a look at GetTickCount example.
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2011
    Posts
    2

    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 ==========

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    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>
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Apr 2010
    Posts
    20

    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
  •  





Click Here to Expand Forum to Full Width

Featured