use time in c++ code example

Example 1: time function c++

/* Answer to: "time function c++" */

#include <chrono> 
using namespace std::chrono; 

auto start = high_resolution_clock::now(); 
auto stop = high_resolution_clock::now(); 
auto duration = duration_cast<microseconds>(stop - start); 
  
cout << duration.count() << endl;

Example 2: time function c++

#include <chrono> 
using namespace std::chrono; 

auto start = high_resolution_clock::now(); 

// perform function to be timed here...

auto stop = high_resolution_clock::now(); 
cout << duration_cast<microseconds>(stop - start).count() << endl;

Example 3: how to get current time in c++

//Simplest way to add time using ctime
	#include <ctime>

	time_t tt;
    time( &tt );
    tm TM = *localtime( &tt );

	//Must add 1 to month and 1900 to the year
    int month=TM.tm_mon+1;
    int day=TM.tm_mday;
    int year=TM.tm_year+1900;

Tags:

Cpp Example