Namespaces
Variants
Views
Actions

std::chrono::high_resolution_clock::now

From cppreference.com
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

 
 
std::chrono::high_resolution_clock
Member functions
high_resolution_clock::now
 
(since C++11)

Returns a time point representing the current point in time.

[edit] Parameters

(none)

[edit] Return value

A time point representing the current time.

[edit] Example

#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:

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