Not all clock_t are equal

Here is a little warning to anyone who might fall into the same trap as me.
 I was using C++ standard clock() from <ctime> to measure the time it took for a program to execute on Windows. I should have read the the MSDN page about it. Visual Studio does not follow the standard for the clock() implementation. It should be measuring the time spent on the CPU but is actually a wall clock.

This was fine for my program as I wanted a wall clock and I was getting sensible results. However when I ran the same program on my Ubuntu machine the data I got back was less than useful.

I wanted to write a cross platform bit of code so I stuck to using standard objects. This hasn't worked out. The MSDN article suggests using GetProcessTimes to get the standard behaviour of clock() but that just isn't good.

I only wanted a wall clock anyway so I had to look for a different solution on both platforms.

The solution is std::chrono which was added in C++11.  It provides all the features I wanted for a wall clock and had somehow slipped past me. I didn't realise this was even part of the C++11 spec. Luckily though, Microsoft has implemented this one correctly and it can be used for getting the wall time on both platforms. Even if it is a little verbose.

#include <chrono>

int main()
{
    std::chrono::time_point<std::chrono::system_clock>  StartTime, EndTime;
    StartTime = std::chrono::system_clock::now();

    EndTime = std::chrono::system_clock::now();
    std::chrono::duration<double> seconds = EndTime - StartTime;

    std::cout << "Time: " << seconds.count() << " seconds.\n";
}