C++ clock() Function: Measuring Execution Time
The 'clock()' function in C++ returns the number of clock ticks elapsed since the program started. Clock ticks are implementation-dependent and may vary from system to system.
To output the result of the 'clock()' function, you can use a 'cout' statement:
#include <iostream>
#include <ctime>
int main() {
clock_t start = clock();
// Code to measure the time
clock_t end = clock();
double duration = (double)(end - start) / CLOCKS_PER_SEC;
std::cout << 'Time elapsed: ' << duration << ' seconds' << std::endl;
return 0;
}
This code measures the time it takes for the code between the 'start' and 'end' variables to execute and then outputs the duration in seconds. Note that you need to include the '
原文地址: http://www.cveoy.top/t/topic/o2Kf 著作权归作者所有。请勿转载和采集!