Search code examples
c++c++11c++-chronocoliru

Why does coliru return the same values for chrono::system_clock::now().time_since_epoch()?


While testing someone else's code on coliru, I noticed that std::chrono::system_clock::now().time_since_epoch() returns the same values on multiple runs. I tested the following code with g++ and clang++ on coliru.

#include <iostream>
#include <chrono>
int main()
{
    auto seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::cout << seed << "\n";
    std::cout << std::chrono::system_clock::now().time_since_epoch().count() << "\n";

    return 0;
}

Output:

g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

1433249917642594133

1433249917642674289

It does work as expected on ideone and obviously on my computer.

Does anyone know why coliru returns the same values on every run?


Solution

  • Coliru caches the results of each snippet, so this is expected behaviour.

    You can force a re-run by trivially altering the source file (by, say, adding more whitespace, or changing the contents of a comment).

    (Source: I know the author.)