Is there any C++ standard class/function which is similar to GetTickCount() on Windows?
Asked Answered
D

2

7
unsigned int Tick = GetTickCount();

This code is running only on Windows, but I want to use the C++ Standard library so it can run elsewhere.

I searched std::chrono, but I can't find a function like GetTickCount().

Do you know what I should use from std::chrono?

Diminution answered 25/7, 2014 at 13:57 Comment(5)
Nothing to get the number of milliseconds since the system was started that I know of, but lots for relative times.Bonnett
Possible duplicate: https://mcmap.net/q/357880/-equivalent-to-gettickcount-on-linux/256138Paulus
C++ doesn't have a notion of "the system started", so, no, there's nothing like it. There are plenty of things in chrono that might solve the same problem you're facing, though. We just need to know what problem you are facing.Beecham
@rubenvb: clock_getttime() is a POSIX standard interface, which isn't portable to Windows any more than GetTickCount() is portable to Linux, so the proposed duplicate is more 'related' than a duplicate.Matsumoto
steady_clock is the nearest equivalent, if you want the elapsed time between two arbitrary points - unlike system_clock it won't be adjusted. But it's no good if you specifically want the time since the system started; there's nothing standard for that.Aultman
A
7

You could build a custom chrono clock on top of Windows' GetTickCount(). Then use that clock. In porting, all you would have to do is port the clock. For example, I am not on Windows, but here is what such a port might look like:

#include <chrono>

// simulation of Windows GetTickCount()
unsigned long long
GetTickCount()
{
    using namespace std::chrono;
    return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
}

// Clock built upon Windows GetTickCount()
struct TickCountClock
{
    typedef unsigned long long                       rep;
    typedef std::milli                               period;
    typedef std::chrono::duration<rep, period>       duration;
    typedef std::chrono::time_point<TickCountClock>  time_point;
    static const bool is_steady =                    true;

    static time_point now() noexcept
    {
        return time_point(duration(GetTickCount()));
    }
};

// Test TickCountClock

#include <thread>
#include <iostream>

int
main()
{
    auto t0 = TickCountClock::now();
    std::this_thread::sleep_until(t0 + std::chrono::seconds(1));
    auto t1 = TickCountClock::now();
    std::cout << (t1-t0).count() << "ms\n";
}

On my system, steady_clock happens to return nanoseconds since boot. You may find other non-portable ways of emulating GetTickCount() on other platforms. But once that detail is done, your clock is solid, and the clock's clients don't need to be any wiser about it.

For me this test reliably outputs:

1000ms
Achromatous answered 25/7, 2014 at 15:27 Comment(0)
L
0

One-line answer,

std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count()
Larrabee answered 19/8, 2024 at 10:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.