Namespaces
Variants
Views
Actions

std::chrono::steady_clock::now

From cppreference.com
< cpp‎ | chrono‎ | steady clock
Revision as of 19:28, 31 May 2013 by P12bot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
(since C++11)

Returns a time point representing with the current point in time.

Contents

[edit] Parameters

(none)

[edit] Return value

A time point representing the current time.

[edit] Exceptions

noexcept specification:  
noexcept
  

[edit] Example

#include <iostream>
#include <vector>
#include <numeric>
#include <chrono>
 
volatile int sink;
int main()
{
    for (unsigned long long size = 1; size < 1000000000ull; size *= 100) {
        // record start time
        auto start = std::chrono::high_resolution_clock::now();
        // do some work
        std::vector<int> v(size, 42);
        sink = std::accumulate(v.begin(), v.end(), 0u); // make sure it's a side effect
        // record end time
        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> diff = end-start;
        std::cout << "Time to fill and iterate a vector of " 
                  << size << " ints : " << diff.count() << " s\n";
    }
}

Possible output:

Time to fill and iterate a vector of 1 ints : 2.43e-07 s
Time to fill and iterate a vector of 100 ints : 4.1e-07 s
Time to fill and iterate a vector of 10000 ints : 2.519e-05 s
Time to fill and iterate a vector of 1000000 ints : 0.00207669 s
Time to fill and iterate a vector of 100000000 ints : 0.423087 s