分析C 程序

Profiling C++ program

本文关键字:程序 分析      更新时间:2023-10-16

我有一个函数可以在时间上配置。实际上,该功能显示在主函数中检查时13222668微秒(13秒)的时间。然后我开始介绍功能的不同部分,最小示例如下:

#include <iostream>
#include <sstream>
#include <string>
#include <chrono>
using namespace std::chrono;
unsigned int match_time;
int func(){ 
  unsigned int i = 0;
  while(i < 20){
   high_resolution_clock::time_point t1 = high_resolution_clock::now(); // if I start the time from here, it shows me just `827687` milliseconds (827 milliseconds). 
   //code....
    {
        //code...
        {
           //code....
        }
          //code....
    }
        high_resolution_clock::time_point t2 = high_resolution_clock::now();
        auto duration = duration_cast<microseconds>( t2 - t1 ).count();
        match_time += duration;
        i++;
    }
    return 0;
   }

如果我在段循环内启动时间,则仅显示827687微秒(827毫秒),但是如果我在while wily loop

上启动
 high_resolution_clock::time_point t1 = high_resolution_clock::now();
 while(i < 20){
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>( t2 - t1 ).count();
match_time += duration;

我有一个高时间13222668微秒(13秒),我对此表示怀疑。

同样,当我检查了不同的语句执行时间时,我会在打印时获得0的时间(我认为这是打印问题),而求和match_time += duration;在数百万次运行中显示了其中的时间(827毫秒)。

我想知道我的时间分析方式是否正确,以及为什么时间在时循环和外部循环(大约是外部循环时间我怀疑)。由于任何其他原因,我的代码是否悬挂了一段时间。

不能只是您的 match_time不是初始化的吗?