#include <iostream>
#include <ctime>
using namespace std;
int main() {
clock_t start, finish;
double duration;
start = clock();
/*실행 시간을 측정하고 싶은 코드*/
finish = clock();
cout << (double)(finish - start) << " ms" << endl;
return 0;
}
high_resolution_clock
을 사용하는게 가장 정확성 높음.#include <chrono>
using namespace std::chrono;
auto start = high_resolution_clock::now();
target_function();
auto stop = high_resolution_clock::now();
// Subtract stop and start timepoints and
// cast it to required unit. Predefined units
// are nanoseconds, microseconds, milliseconds,
// seconds, minutes, hours. Use duration_cast()
// function.
auto duration = duration_cast<microseconds>(stop - start);
// To get the value of duration use the count()
// member function on the duration object
cout << duration.count() << endl;
출처
- https://sroongzi.tistory.com/entry/c-%EC%8B%A4%ED%96%89%EC%8B%9C%EA%B0%84-%EC%B8%A1%EC%A0%95-%EB%B0%A9%EB%B2%95-time-clock?_sc_token=v2%253AGtIhx6sYUiQD-2eucK0IIrJB9QNRiOy3mUHlzS5glLkuGY0N1YimJ7h32i_L4NaVc1vygKTMydsKo444NK3-2t-XbStcCuvmNXJ0A_pLs7L1EJmpr3VjEmHt89Di7xDlWSchwhX9-7H681OawLLDxmqMtuBz3x8JEG2oZwjVUTI%253D
- https://www.geeksforgeeks.org/measure-execution-time-function-cpp/