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

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

πŸ’… NAME πŸ’…

std::chrono::high_resolution_clock::now β€” and yeah, the man page literally lists std::chrono::high_resolution_clock::now a second time right after the dash, no extra sauce, that's just how she filed herself. Periodt.

This page lives in the C++ Standard Libary (yes "Libary", the typo is canon, I don't make the rules πŸŽ€), section 3 of the manual β€” and the header at the top of the page is genuinely truncated into std::chrono...n_clock::now(3) because the title got cut off mid-word. Iconic behavior, honestly. Anyway. Bestie. It's a clock. It tells you now. Let's go πŸ•


✨ Synopsis ✨

The man page formatting did us SO dirty here, so here's exactly what got printed on the page, verbatim, scrambled eggs and all 😭:

     static std::chrono::time_point<std::chrono::high_resolution_clock>
  now()     (since
     noexcept;
  C++11)

...and here's the same thing un-scrambled so your brain can vibe check it 🫢:

static std::chrono::time_point<std::chrono::high_resolution_clock> now() noexcept;
(since C++11)

🧚 what that actually means, lowkey

  • static β€” it's a static member function, so no object needed. You don't instantiate anything, you just call it.
  • std::chrono::time_point<std::chrono::high_resolution_clock> β€” that's the return type, a whole time point parameterized on the high resolution clock.
  • now() β€” the function name. No arguments.
  • noexcept β€” it's promising not to throw. A well-behaved queen.
  • (since C++11) β€” she's been around since C++11. An elder icon.
Heads up: that's the entire synopsis. No overloads hiding in the back, no secret extra params. Just now().

🌸 Description 🌸

Returns a time point representing the current point in time. That's it. That's the whole job πŸ’–


πŸ¦‹ Parameters πŸ¦‹

(none) β€” literally zero arguments. Nada. She takes nothing, she needs nothing, she simply knows what time it is.


⭐ Return value ⭐

A time point representing the current time. Slay πŸ’…


πŸ”₯ Example πŸ”₯

The page says // Run this code, so here's the code, exactly as it appears, bestie β€” don't let the wrapped lines jump-scare you, that's just the man page being narrow:

        #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::high_resolution_clock::now();
                do_some_work(size);
                const auto end = std::chrono::high_resolution_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 πŸŽ€

Note the word possible β€” this is one run, not a promise, your numbers may lowkey differ:

        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

And that's the whole page, bestie 🫢 No SEE ALSO, no bugs section, no author credits β€” she's short and she knows it.

Cute little sign-off for the record: this page came from http://cppreference.com, dated 2024.06.10, and it's filed under std::chrono...n_clock::now(3) β€” same truncated-name situation as the header. Bye bye, go time something, I'm obsessed with you getting it right πŸ’œβ±οΈ

πŸ“¦ where this page comes from