std::chrono...c_clock::now(3)
π 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms