Time

1. time output

#include <ctime>
#include <chrono>
#include <iostream>
using namespace std;
using namespace chrono;
int main()
{
    auto currentTimePoint = system_clock::now();
    auto currentTime = system_clock::to_time_t( currentTimePoint );
    auto timeText = ctime( &currentTime );
    cout << timeText << endl;
}

2. compare times

#include <ctime>
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
using namespace chrono;
using namespace literals;
int main()
{
    auto startTimePoint = system_clock::now();
    this_thread::sleep_for(5s);
    auto endTimePoint = system_clock::now();
    auto timeTaken = duration_cast(endTimePoint - startTimePoint);
    cout << "Time Taken: " << timeTaken.count() << endl;
    return 0;
}

3. duration


auto start = std::chrono::high_resolution_clock::now();
do_something();
auto stop = std::chrono::high_resolution_clock::now();
std::cout << "do something took " << std::chrono::duration<double, std::chrono::seconds>(stop-start).count() << " seconds" << std::endl;

some useful tips (mostly for myself)