[英]How to time a methods execution using chrono in C++?
所以這是我第一次嘗試進行性能測量,並在我嘗試我的課程作業之前遵循一些在線資源來測試我的代碼的較小版本。 不幸的是,我無法打印完成該功能所需的時間,我不確定我是否做得對。
#include <string>
#include <iostream>
#include <unordered_map>
#include <chrono>
using namespace std;
class Timer {
public:
Timer() {
startTimept = std::chrono::high_resolution_clock::now();
}
~Timer() {
Timer Stop();
}
void Stop() {
auto endTimept = std::chrono::high_resolution_clock::now();
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(startTimept).time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimept).time_since_epoch().count();
auto duration = end - start;
double ms = duration * 0.001;
std::cout << duration << "us (" << ms << "ms)";
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> startTimept;
};
int main()
{
std::unordered_map<std::string, std::string>::iterator found, start, nFound;
//ADDS PAIRS OF SENTENCE INTO A MAP
std::unordered_map<std::string, std::string> sortMap =
{ { "these", "pairs" }, { "the", "correct" }, { "pairs", "makes" }, { "correct", "sentence" }, { "makes", "the" } };
std::unordered_map<std::string, std::string> swapMap =
{ { "pairs","these" }, {"correct", "the"}, { "makes", "pairs" }, {"sentence", "correct" }, {"the", "makes"} };
//CREATES CONTAINER TO STORE COMPLETE SENTENCE
std::list<std::string> resultSeq;
start = sortMap.begin();
//ADD STARTING WORDS INTO THE LIST
resultSeq.push_back(start->first);
resultSeq.push_back(start->second);
//TEMP POINTER TO SOUGHT WORD
found = sortMap.find(start->second);
//THIS IS THE FUNCTION I AM TRYING TO TEST
{
Timer timer();
for (auto it = sortMap.begin(); it != sortMap.end(); ++it) {
if (it == found) {
resultSeq.push_back(it->second);
found = sortMap.find(it->second);
it = sortMap.begin();
}
}
}
for (std::list<std::string>::iterator hard = resultSeq.begin(); hard != resultSeq.end(); ++hard)
{
std::cout << (*hard) << std::endl;
}
__debugbreak;
}
如果有人能發現我做錯了什么或提供任何鏈接來幫助衡量性能,那將非常有幫助!
您應該使用std::steady_clock
來測量時差。 std::system_clock
可以是任何東西,不需要是單調的。 std::high_resolution_clock
。
high_resolution_clock 在不同標准庫實現中的實現不一致,應避免使用它。 它通常只是 std::chrono::steady_clock 或 std::chrono::system_clock 的別名,但它是哪個取決於庫或配置。 當它是 system_clock 時,它不是單調的(例如,時間可以倒退)。 例如,對於 gcc 的 libstdc++,它是 system_clock,對於 MSVC,它是 stable_clock,而對於 clang 的 libc++,它取決於配置。
此外,為了封裝時間測量,您可以利用 C++ 的 RAII 機制:
#include <chrono>
#include <type_traits>
#include <iostream>
template <typename Resolution = std::chrono::duration<double,std::micro>>
class Stopwatch {
typedef std::chrono::steady_clock Clock;
private:
std::chrono::time_point<Clock> last;
public:
void reset() noexcept {
last = Clock::now();
}
Stopwatch() noexcept {
reset();
}
auto operator()() const noexcept {// returns time in Resolution
return Resolution(Clock::now() - last).count();
}
~Stopwatch() {
// bad encapulation, you should reconsider this design!
// e.g. take a std::function as argument to the constructor and call it here
std::cout << (*this)() << "\n";
}
};
int main() {
{ // the block I want to measure
Stopwatch sw;
(void)(1+2+3); // some costly operation
} // end of measured block
}
我個人的方法,如果只是為了快速調試目的,將使用std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
. 由於它非常冗長,我建議為它制作一個包裝函數。 無論如何,這只是返回自 1970 年以來的時間(以毫秒為單位)(不確定)。 然后,你只要堅持一個函數之前和一個后, cout
的差異,你有毫秒的功能了執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.