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

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

πŸ’… std::chrono::utc_clock::now πŸ’…

okay bestie, welcome to the C++ Standard Libary section of the manual (yes, that's the manual name, we don't fix typos here, we celebrate them ✨). Today's main character is std::chrono::utc_clock::now, and it's giving "I just tell you what time it is, UTC edition" πŸ•°οΈ


🌸 NAME 🌸

std::chrono::utc_clock::now β€” literally std::chrono::utc_clock::now, that's the whole name, no notes πŸ’…


✨ Synopsis ✨

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

So here's the tea πŸ«–: this bad boy returns a time point representing the current point in time. And the way it gets that value? It's calculated as if by:

std::chrono::utc_clock::from_sys(std::chrono::system_clock::now())

no cap. But β€” and this is the lowkey flex β€” implementations may use a more accurate value of UTC time πŸ‘‘ so it might be even more precise than that, iykyk.

πŸ¦‹ Important caveat, bestie: "as if by" is the vibe check here. The standard describes it that way, but implementations are allowed to be fancier about it. Don't go assuming it's literally calling system_clock::now() under the hood, fr fr.

πŸŽ€ Parameters πŸŽ€

(none)

That's right β€” zero arguments. It takes nothing and gives you time. Iconic behavior honestly πŸ’œ


πŸ’– Return value πŸ’–

A time point representing the current time. That's it, that's the whole deal 🌈


πŸ“ Example πŸ“

// Run this code
        #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::utc_clock::now();
                do_some_work(size);
                const auto end = std::chrono::utc_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';
            }
        }

Bestie it's so simple β€” grab utc_clock::now(), do the work, grab utc_clock::now() again, subtract, and suddenly you're a benchmarking girlie πŸ“Šβœ¨ (the volatile int sink is there so the compiler can't just delete all your hard work, cute right? 🧚)

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

(the keyword is possible, bestie β€” it says possible output, so please don't come for me when your numbers don't match, different machines, different vibes πŸ’€)


and that's the page! 🫢 sign-off time: this little slice of heaven is brought to you by http://cppreference.com, dated 2024.06.10, and filed under std::chrono...c_clock::now(3) in the manual. mwah πŸ’‹ periodt. 🌟

πŸ“¦ where this page comes from