How to use clock() in C++
问题 How do I call clock() in C++ ? For example, I want to test how much time a linear search takes to find a given element in an array. 回答1: #include <iostream> #include <cstdio> #include <ctime> int main() { std::clock_t start; double duration; start = std::clock(); /* Your algorithm here */ duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; std::cout<<"printf: "<< duration <<'\n'; } 回答2: An alternative solution, which is portable and with higher precision, available since C++11, is