C++でミリ秒単位まで時間を取得する方法は何ですか?

C++では、ヘッダーファイルのstd::chrono::high_resolution_clockを使用して、ミリ秒単位の正確な時間を取得できます。以下は簡単なサンプルコードです。

#include <iostream>
#include <chrono>

int main() {
    auto start = std::chrono::high_resolution_clock::now();
    
    // 这里放置需要测量时间的代码块
    
    auto end = std::chrono::high_resolution_clock::now();
    
    std::chrono::duration<double, std::milli> duration = end - start;
    
    std::cout << "Time taken: " << duration.count() << " milliseconds" << std::endl;
    
    return 0;
}

上記の例では、high_resolution_clock::now() は時間点を返し、その後、2つの時間点の差を計算し、最終的にその差をミリ秒単位に変換して出力します。

广告
広告は10秒後に閉じます。
bannerAds