【C++】计算时间差

1.clock()

标准库中ctime头文件
#include <ctime>
#include <iostream>

using namespace std;

int main(){
    clock_t start = clock();      //获取当前系统时间
    
    function();

    clock_t end  = clock();

    double programTimes = ((double) end -start) / CLOCKS_PER_SEC;
}

2.high_resolution_clock

chrono (C++11新增程序库) 
#include <chrono>
#include <iostream>

using namespace std;

int main(){
    auto beginTime = std::chrono::high_resolution_clock::now();

    function();

    auto endTime = std::chrono::high_resolution_clock::now();
    auto elapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(endTime-beginTime);

    double programTimes = ((double) elapsedTime.count();
}