久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 C++ 中測(cè)量函數(shù)的執(zhí)行時(shí)間

Measuring execution time of a function in C++(在 C++ 中測(cè)量函數(shù)的執(zhí)行時(shí)間)
本文介紹了在 C++ 中測(cè)量函數(shù)的執(zhí)行時(shí)間的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想了解某個(gè)函數(shù)在我的 C++ 程序中在 Linux 上執(zhí)行所需的時(shí)間.之后,我想做一個(gè)速度比較.我看到了幾個(gè)時(shí)間函數(shù),但最終從 boost 得到了這個(gè).時(shí)間:

I want to find out how much time a certain function takes in my C++ program to execute on Linux. Afterwards, I want to make a speed comparison . I saw several time function but ended up with this from boost. Chrono:

process_user_cpu_clock, captures user-CPU time spent by the current process

現(xiàn)在,我不清楚是否使用上述功能,我是否會(huì)獲得 CPU 在該功能上花費(fèi)的唯一時(shí)間?

Now, I am not clear if I use the above function, will I get the only time which CPU spent on that function?

其次,我找不到任何使用上述功能的示例.任何人都可以幫助我如何使用上述功能嗎?

Secondly, I could not find any example of using the above function. Can any one please help me how to use the above function?

PS:現(xiàn)在,我正在使用 std::chrono::system_clock::now() 以秒為單位獲取時(shí)間,但是由于每次 CPU 負(fù)載不同,這會(huì)給我不同的結(jié)果.

P.S: Right now , I am using std::chrono::system_clock::now() to get time in seconds but this gives me different results due to different CPU load every time.

推薦答案

它是 C++11 中非常易于使用的方法.您必須使用 標(biāo)頭中的 std::chrono::high_resolution_clock.

It is a very easy-to-use method in C++11. You have to use std::chrono::high_resolution_clock from <chrono> header.

像這樣使用它:

#include <chrono>

/* Only needed for the sake of this example. */
#include <iostream>
#include <thread>
    
void long_operation()
{
    /* Simulating a long, heavy operation. */

    using namespace std::chrono_literals;
    std::this_thread::sleep_for(150ms);
}

int main()
{
    using std::chrono::high_resolution_clock;
    using std::chrono::duration_cast;
    using std::chrono::duration;
    using std::chrono::milliseconds;

    auto t1 = high_resolution_clock::now();
    long_operation();
    auto t2 = high_resolution_clock::now();

    /* Getting number of milliseconds as an integer. */
    auto ms_int = duration_cast<milliseconds>(t2 - t1);

    /* Getting number of milliseconds as a double. */
    duration<double, std::milli> ms_double = t2 - t1;

    std::cout << ms_int.count() << "ms
";
    std::cout << ms_double.count() << "ms";
    return 0;
}

這將測(cè)量函數(shù)long_operation的持續(xù)時(shí)間.

This will measure the duration of the function long_operation.

可能的輸出:

150ms
150.068ms

工作示例:https://godbolt.org/z/oe5cMd

這篇關(guān)于在 C++ 中測(cè)量函數(shù)的執(zhí)行時(shí)間的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類(lèi)型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時(shí)候使用內(nèi)聯(lián)函數(shù),什么時(shí)候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關(guān)嗎?)
主站蜘蛛池模板: 九色国产 | 懂色中文一区二区在线播放 | 国产免国产免费 | 妹子干综合 | 午夜精品一区二区三区在线观看 | 欧美成人精品 | 在线观看免费av网 | 久久久久久国产 | 黄色精品视频网站 | 免费污视频 | 91高清免费 | 日本久草 | 中文字幕日韩欧美一区二区三区 | 9191av | 亚洲国产成人久久综合一区,久久久国产99 | 久久久久综合 | 欧美一区二区三区在线观看视频 | av网站在线播放 | 欧美亚州| 91欧美激情一区二区三区成人 | 亚洲精品精品 | 午夜小视频在线播放 | 一区二区在线观看免费视频 | 精品一区二区三区在线播放 | 久久99精品久久久久久国产越南 | 欧美极品在线 | 欧美99久久精品乱码影视 | 欧美激情在线精品一区二区三区 | 久久久网 | 亚洲 欧美 日韩在线 | 久久精品视频网站 | 亚洲日韩中文字幕一区 | 插插宗合网 | 免费成人高清在线视频 | 亚洲精品国产精品国自产在线 | 国产成人叼嘿视频在线观看 | 成人激情视频免费在线观看 | 国内精品久久久久久久影视简单 | 亚洲精品一二三区 | 国产a级毛毛片 | 日日操日日舔 |