Writing a class for a timer with GetTickCount to time between two lines of code.

Hi guys!

I'm new to posting, and new to programming, so I apologise beforehand!
I have a number of things to do.

I'm trying to write a VERY simple class for a timer, and I need to time between two lines of code (these lines of code haven't been written yet. They can be anything simple at all.) The class needs a start and stop function, and the stop function needs to return the delay that was measured. I've been told to do this using the GetTickCount function. I understand that QueryPerformanceCounter may be more suited in this situation, but my training specifies using GetTickCount.

I'd really appreciate any help at all.

Thank's for taking the time in reading this!
So does this look like something you might understand?
1
2
3
4
5
class fooTimer() {
public:
    void start();
    long stop();
};
here is a crude one. Being lazy and uninspired, its just a struct wrapper around the timer example I found online. It seems to measure e-7 seconds ish resolution on my box

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

using namespace std::chrono;

struct hrtimer
{
 high_resolution_clock::time_point s; 
 duration<double> time_span;
 void start(){s = high_resolution_clock::now();}
 double stop()
 {    
   time_span = duration_cast<duration<double>>( high_resolution_clock::now() - s);
   return time_span.count();
 }
};

Last edited on
^ just to clarify this, C++11 already has its version of a "high-resolution clock" (as shown above), so no need to use platform-specific Windows API (GetTickCount or QueryPerformanceTimer), unless your needs are more specific.

https://stackoverflow.com/questions/14391327/how-to-get-duration-as-int-millis-and-float-seconds-from-chrono
https://stackoverflow.com/questions/49090366/how-to-convert-stdchronohigh-resolution-clocknow-to-milliseconds-micros
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <chrono>
#include <ctime>
#include <type_traits>
#include <thread>

struct timer
{
    using wall_clock = std::conditional< std::chrono::high_resolution_clock::is_steady,
                                         std::chrono::high_resolution_clock,
                                         std::chrono::steady_clock >::type ;

    void reset() { start_w = wall_clock::now() ; start_p = std::clock() ; }

    unsigned long long milliseconds_elapsed() const
    {
        const auto now = wall_clock::now() ;
        using namespace std::chrono ;
        return duration_cast<milliseconds>(now-start_w).count() ;
    }

    double milliseconds_processor() const
    {
        const auto now = std::clock() ;
        return (now-start_p) * 1000.0 / CLOCKS_PER_SEC ;
    }

    wall_clock::time_point start_w = wall_clock::now() ;
    std::clock_t start_p = std::clock() ;
};

int main()
{
    timer t ;

    for( volatile long long i = 0; i < 100'000'000 ; ++i ) ;

    std::this_thread::sleep_for( std::chrono::milliseconds(1500) ) ;

    std::cout << "  elapsed milliseconds: " << t.milliseconds_elapsed() << '\n'
              << "processor milliseconds: " << t.milliseconds_processor() << '\n' ;
}

http://coliru.stacked-crooked.com/a/fcf76cd60b266b6c
Topic archived. No new replies allowed.