std::chrono...i_clock::now(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

std::chrono::tai_clock::now β€” okay so this is the manpage for std::chrono::tai_clock::now, bestie. That's it, that's the name section, we love a self-titled icon πŸ‘‘ (it's from the C++ Standard Library manual, section 3, in case you were wondering 🫢).


✨ Synopsis ✨

static std::chrono::time_point<std::chrono::tai_clock> now();  (since C++20)

So real talk: this bad boy hands you back a time point that represents the current point in time. Like, right now. Currently. As of this exact moment πŸ’….

The way it works is it's calculated as if by this call:

std::chrono::tai_clock::from_utc(std::chrono::utc_clock::now())

But β€” and this is the tea 🍡 β€” implementations are allowed to use a more accurate value of TAI time if they've got one. So it's giving "at least this good, maybe better," no cap.


πŸŽ€ Parameters πŸŽ€

(none) β€” literally zero arguments, she takes nothing, she just is ✨. The easiest function call of your entire life, you're welcome.


🌸 Return value 🌸

A time point representing the current time πŸ•. That's the whole vibe. You asked for now, you got now.


πŸ¦‹ Example πŸ¦‹

Okay this example is adorable, it's just timing how long it takes to fill and iterate a vector of ints, at increasing sizes. Run this code, bestie:

#include <chrono>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

volatile int sink;

void do_some_work(std::size_t size)
{
    std::vector<int> v(size, 42);
    sink = std::accumulate(v.begin(), v.end(), 0); // make sure it is a side effect
}

int main()
{
    std::cout << std::fixed << std::setprecision(9) << std::left;
    for (auto size{1ull}; size < 10'00'00'00'00ull; size *= 100)
    {
        const auto start = std::chrono::tai_clock::now();
        do_some_work(size);
        const auto end = std::chrono::tai_clock::now();

        const std::chrono::duration<double> diff = end - start;

        std::cout << "Time to fill and iterate a vector of " << std::setw(9)
                  << size << " ints : " << diff << '\n';
    }
}

Possible output:

Time to fill and iterate a vector of 1         ints : 0.000006568s
Time to fill and iterate a vector of 100       ints : 0.000002854s
Time to fill and iterate a vector of 10000     ints : 0.000116290s
Time to fill and iterate a vector of 1000000   ints : 0.011742752s
Time to fill and iterate a vector of 100000000 ints : 0.505534949s

Look at that progression 😭 the 1 int case is literally free and the 100000000 case is out here taking half a second. volatile int sink is the real MVP though β€” she exists purely so the optimizer can't just delete all your hard work, periodt πŸ”₯.

Heads up: that output is possible output, not promised output β€” your machine is gonna do its own thing, and that's fine πŸ’–.

Okay that's the whole page, you're officially educated 🧚 Go forth and timestamp things. Sending you good vibes and no utc_clock regressions β€” this was std::chrono...i_clock::now(3) from the C++ Standard Libary manual, straight off http://cppreference.com, dated 2024.06.10 πŸŽ€βœ¨πŸ’œ

πŸ“¦ where this page comes from