17

I am new to C++ and I don't know much about its library. I need to do time analysis of different sorting algorithms, for which I need to get the current time in milliseconds. Is there any way to do that?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Yasir Mustafa
  • 173
  • 1
  • 1
  • 5
  • 1
    look into std::chorno – DanielCollier Dec 10 '16 at 15:56
  • For analysis of sorting algorithms, milliseconds may not be low enough resolution, and "current time" isn't really what you need anyway. You want relative time it takes to perform a task. `std::chrono::high_resolution_clock` is probably what you want. – Chad Dec 10 '16 at 16:00
  • Thanks gsamaras for the solution, just small doubt, as someone above in the comments mentioned that current time is an relative time, in java current time is calculated from midnight 1sr Jan 1970, so high_resolution_clock::now() function returns time relative to which time? – Yasir Mustafa Dec 10 '16 at 17:55
  • @YasirMustafa you are welcome! Your question good. As for the question asked now, see my edit, the epoch (εποχή) is the same in [tag:c++] as well: `Thu Jan 01 01:00:01 1970`. – gsamaras Dec 10 '16 at 18:40
  • 2
    Possible duplicate of [How to print current time (with milliseconds) using C++ / C++11](https://stackoverflow.com/questions/16077299/how-to-print-current-time-with-milliseconds-using-c-c11) – Ciro Santilli OurBigBook.com Dec 10 '17 at 19:03

3 Answers3

34

Simply use std::chrono. The general example below times the task "of printing 1000 stars":

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
 
int main ()
{
  using namespace std::chrono;
 
  high_resolution_clock::time_point t1 = high_resolution_clock::now();
 
  std::cout << "printing out 1000 stars...\n";
  for (int i=0; i<1000; ++i) std::cout << "*";
  std::cout << std::endl;
 
  high_resolution_clock::time_point t2 = high_resolution_clock::now();
 
  duration<double, std::milli> time_span = t2 - t1;
 
  std::cout << "It took me " << time_span.count() << " milliseconds.";
  std::cout << std::endl;
 
  return 0;
}

Instead of printing the stars, you will place your sorting algorithm there and time measure it.


Do not forget to enable the optimization flags for your compiler, if you intend to do some benchmarking, e.g. for , you need -O3. This is serious, check what happened to me when I didn't do so: Why emplace_back is faster than push_back?


Ps: If your compiler doesn't support , then you could look into other methods in my Time Measurements (C++).


A specific (toy) example, by using my Quicksort (C++), would be:

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

void quickSort(int a[], int first, int last);
int pivot(int a[], int first, int last);
void swap(int& a, int& b);
void swapNoTemp(int& a, int& b);

using namespace std;
using namespace std::chrono;

int main()
{
    int test[] = { 7, -13, 1, 3, 10, 5, 2, 4 };
    int N = sizeof(test)/sizeof(int);

    cout << "Size of test array :"  << N << endl;

    high_resolution_clock::time_point t1 = high_resolution_clock::now();

    // I want to measure quicksort
    quickSort(test, 0, N-1);

    high_resolution_clock::time_point t2 = high_resolution_clock::now();

    duration<double> time_span = t2 - t1;

    std::cout << "It took me " << time_span.count() << " seconds.";
    std::cout << std::endl;

    return 0;
}

and the output now is:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++11 -O3 main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
Size of test array :8
It took me 3.58e-07 seconds.

It's as simple as that. Happy benchmarking! =)


EDIT:

high_resolution_clock::now() function returns time relative to which time?

From std::chrono:

Time points

A reference to a specific point in time, like one's birthday, today's dawn, or when the next train passes. In this library, objects of the time_point class template express this by using a duration relative to an epoch (which is a fixed point in time common to all time_point objects using the same clock).

where one could check this epoch and time_point example, which outputs:

time_point tp is: Thu Jan 01 01:00:01 1970
Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 2
    You can simplify your first block of code by assigning into a double-based-millisecond with no explicit cast: `duration time_span = t2 - t1;`. And then you can print this out with no manual conversion to milliseconds: `<< time_span.count() << " milliseconds."`. You can also drop the explicit `duration_cast` in your second example. Other tips for writing more reliable `chrono` code are here: https://www.youtube.com/watch?v=P32hvk8b13M – Howard Hinnant Dec 10 '16 at 20:59
1

Simple working example

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

void longTask(){
    for(auto i = 0; i < INT_MAX; i++){
        //do something;
    }
}
int main(){
    auto startTime = high_resolution_clock().now();
    longTask();
    auto stopTime = high_resolution_clock().now();

    //Warning: can't print startTime or stopTime to cout withoutcasting
    //microseconds and milliseconds allowed types in duration_cast

    auto duration = duration_cast<milliseconds>(stopTime - startTime); 
    cout << "Time take to run longTask() in milliseconds: " << duration.count();
    return 0;
}

Output

Time take to run longTask() in milliseconds: 5274

Enjoy !!

Om Sao
  • 7,064
  • 2
  • 47
  • 61
0

As you simply asked for a way to get the current time just use the following function:

#include <chrono>

long double curtime() {
  return std::chrono::duration_cast<std::chrono::milliseconds>(
    std::chrono::system_clock::now().time_since_epoch()
  ).count();
}

Use it like this:

int main() {
  long double begin = curtime();
  // do some crazy stuff
  long double end = curtime();
  std::cout << "Executed in " << end - begin << " milliseconds" << std::endl;
  return 0; 
}
Ayush
  • 457
  • 8
  • 16