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

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

πŸ›°οΈ NAME πŸ›°οΈ

std::chrono::gps_clock::now β€” bestie, it's literally std::chrono::gps_clock::now, the function that hands you "right now" but in GPS time πŸ’…βœ¨

This is a page from the C++ Standard Library manual, and honestly? Iconic behaviour only.


🌸 Synopsis 🌸

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

Okay so what this does πŸ‘‘: it returns a time point representing the current point in time. That's it, that's the whole vibe.

And the way the result gets calculated? It's as if by:

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

Lowkey a relay race between clocks πŸƒβ€β™€οΈ ngl. But heads up β€” implementations may use a more accurate value of GPS time, so don't come for them if they're out here being extra precise πŸŽ€


πŸ¦‹ Parameters πŸ¦‹

  • (none) β€” literally nothing, she takes zero arguments, main character energy fr fr ✨

⭐ Return value ⭐

A time point representing the current time. Periodt. 🫢


πŸ’œ 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::gps_clock::now();
        do_some_work(size);
        const auto end = std::chrono::gps_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';
    }
}

Like, the whole thing is just: grab the time, do some work, grab the time again, subtract, print πŸ’– A little start / end / diff sandwich, iykyk πŸ₯ͺ The volatile int sink is there so the compiler can't be like "nah I'll just delete your work" β€” it's making sure there's a side effect, no cap.


πŸ”₯ 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 scaling go πŸ“ˆ not the numbers just vibing all pretty in a column. Note the word possible β€” your numbers will be different depending on your machine, so don't @ me 😭


πŸŽ€ Sign-off πŸŽ€

Source and version: http://cppreference.com, dated 2024.06.10, and the page is std::chrono...s_clock::now(3) β€” go forth and time things, bestie, you're gonna slay πŸ§šβ€β™€οΈπŸ’œ

πŸ“¦ where this page comes from