/* * This program demonstrates how to calculate the actual * processing time of a part of your program, say the * execution time of function f(). The program uses the * function clock(), the clock_t type, and the constant * CLOCKS_PER_SEC defined in the C++ standard library "ctime". * * Note: You can also use C's gettimeofday() function for timing, instead of this. */ // be sure to include #include #include using namespace std; int main(int argc,char *argv[]) { /* * We use the clock() program in library . Program clock() * returns the number of clock ticks elapsed since the process * starts. The number of clock ticks is represented by type clock_t. * * If you wonder why we don't use time(), it's because time() * returns a value with a precision only to seconds. */ /* record the number of elapsed ticks before calling f() */ clock_t start=clock(); // ---> call f() here <--- /* record the number of elapsed ticks after calling f() */ clock_t end=clock(); /* * The number of ticks elapsed during the f() function call * can be calculated by (end - start). The constant * CLOCKS_PER_SEC specifies the relation between a clock tick * and a second (i.e. clock ticks per second). Therefore, dividing * (end - start) by CLOCKS_PER_SEC will give you the elapsed * time in seconds. */ cout << "CPU elapsed time in seconds: " <<(double)(end - start)/CLOCKS_PER_SEC < call f() here <--- clock_t end=clock(); cout << "CPU elapsed time in seconds: " <<(double)(end - start)/CLOCKS_PER_SEC <