Answers:
#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';
}
              clock()和@ clock_t均来自C标准库的标头time.h,因此std在包含它们的库之后不需要使用名称空间前缀。<ctime>用std名称空间包装该值和函数,但不需要使用。在此处查看实施详情:en.cppreference.com/w/cpp/header/ctime
                    自C ++ 11起提供了一种可移植且精度更高的替代解决方案,即使用std::chrono。
这是一个例子:
#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;
int main()
{
    auto t1 = Clock::now();
    auto t2 = Clock::now();
    std::cout << "Delta t2-t1: " 
              << std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count()
              << " nanoseconds" << std::endl;
}
在ideone.com上运行它给了我:
Delta t2-t1: 282 nanoseconds
              using Clock=std::chrono::high_resolution_clock;。请参阅类型别名。
                    std::chrono::high_resolution_clock在所有标准库实现中不是单调的。从cppreference- 通常,应该只使用std :: chrono :: steady_clock或std :: chrono :: system_clock而不是std :: chrono :: high_resolution_clock:使用steady_clock进行持续时间测量,使用system_clock进行挂钟时间。
                    clock()返回自程序启动以来的时钟滴答数。有一个相关的常数,CLOCKS_PER_SEC它告诉您在一秒钟内发生了多少个时钟滴答。因此,您可以测试以下任何操作:
clock_t startTime = clock();
doSomeOperation();
clock_t endTime = clock();
clock_t clockTicksTaken = endTime - startTime;
double timeInSeconds = clockTicksTaken / (double) CLOCKS_PER_SEC;
              timeInSeconds总是0.000000为我而来。我该如何解决?
                    long double获得更高的精度。
                    至少在Windows上,唯一实用的精确测量机制是QueryPerformanceCounter(QPC)。std :: chrono是使用它实现的(自VS2015起,如果使用的话),但是它的准确性与直接使用QueryPerformanceCounter的精度不一样。特别是它声称以1纳秒的粒度报告绝对是不正确的。因此,如果您要测量花费很短时间的东西(并且您的情况可能只是这种情况),则应使用QPC或与您的操作系统等效的方法。在测量缓存延迟时,我遇到了这个问题,在这里写下了一些您可能会觉得有用的注释。 https://github.com/jarlostensen/notesandcomments/blob/master/stdchronovsqcp.md
#include <iostream>
#include <ctime>
#include <cstdlib> //_sleep()  --- just a function that waits a certain amount of milliseconds
using namespace std;
int main()
{
    clock_t cl;     //initializing a clock type
    cl = clock();   //starting time of clock
    _sleep(5167);   //insert code here
    cl = clock() - cl;  //end point of clock
    _sleep(1000);   //testing to see if it actually stops at the end point
    cout << cl/(double)CLOCKS_PER_SEC << endl;  //prints the determined ticks per second (seconds passed)
    return 0;
}
//outputs "5.17"
              也许您可能对这样的计时器感兴趣:H:M:S。毫秒
Linux OS中的代码:
#include <iostream>
#include <unistd.h>
using namespace std;
void newline(); 
int main() {
int msec = 0;
int sec = 0;
int min = 0;
int hr = 0;
//cout << "Press any key to start:";
//char start = _gtech();
for (;;)
{
        newline();
                if(msec == 1000)
                {
                        ++sec;
                        msec = 0;
                }
                if(sec == 60)
                {
                        ++min;
                        sec = 0; 
                }
                if(min == 60)
                {
                        ++hr;
                        min = 0;
                }
        cout << hr << " : " << min << " : " << sec << " . " << msec << endl;
        ++msec;
        usleep(100000); 
}
    return 0;
}
void newline()
{
        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
}
              usleep不会总是在要求的数量之后返回。有时会更长。您应该在开始时检查当前时间,然后检查当前时间并减去以获取自每次循环开始以来的绝对时间。
                    您可以衡量程序的运行时间。以下功能有助于测量自程序启动以来的CPU时间:
我的参考资料:算法工具箱第一周课程的数据结构和算法专业由加利福尼亚大学圣地亚哥分校和美国国立研究大学高等经济学院组成
因此您可以在算法之后添加此行代码
cout << (double)clock() / CLOCKS_PER_SEC ;
预期输出:代表数量的输出 clock ticks per second
perf stat -d ./a.out