C++ 语句执行的计时-C++;

C++ 语句执行的计时-C++;,c++,benchmarking,C++,Benchmarking,可能重复: 类似于Linux下的time实用程序,但仅适用于某些特定语句。这里有一段非常好的代码片段,在windows和Linux上运行良好: 要使用它,请运行它并将结果保存为“开始时间”和操作后的“结束时间”。减法和除法可以达到你需要的精度。你需要编写一个简单的计时系统。C++中没有内置的方法。 #include <sys/time.h> class Timer { private: struct timeval start_t; public: double

可能重复:

<如何在C++代码中获得特定语句集所花费的时间?< /P>
类似于Linux下的
time
实用程序,但仅适用于某些特定语句。

这里有一段非常好的代码片段,在windows和Linux上运行良好:

要使用它,请运行它并将结果保存为“开始时间”和操作后的“结束时间”。减法和除法可以达到你需要的精度。

你需要编写一个简单的计时系统。C++中没有内置的方法。
#include <sys/time.h>

class Timer
{
private:
    struct timeval start_t;
public:
    double start() { gettimeofday(&start_t, NULL); }
    double get_ms() {
       struct timeval now;
       gettimeofday(&now, NULL);
       return (now.tv_usec-start_t.tv_usec)/(double)1000.0 +
              (now.tv_sec-start_t.tv_sec)*(double)1000.0;
    }
    double get_ms_reset() {
      double res = get_ms();
      reset();
      return res;
    }
    Timer() { start(); }
};

int main()
{
  Timer t();
  double used_ms;

  // run slow code..
  used_ms = t.get_ms_reset();

  // run slow code..
  used_ms += t.get_ms_reset();
  return 0;
}
#包括
班级计时器
{
私人:
结构时间值开始;
公众:
double start(){gettimeofday(&start_t,NULL);}
双重获得{
现在构造timeval;
gettimeofday(&now,NULL);
return(now.tv\u usec-start\u t.tv\u usec)/(double)1000.0+
(现在是.tv_sec-start_t.tv_sec)*(双倍)1000.0;
}
双重获取\u ms\u重置(){
double res=get_ms();
重置();
返回res;
}
计时器(){start();}
};
int main()
{
定时器t();
双重使用;
//运行慢代码。。
used_ms=t.get_ms_reset();
//运行慢代码。。
used_ms+=t.get_ms_reset();
返回0;
}

请注意,度量本身会显著影响运行时。

您可以使用
#inclide
标题。假设您想查看代码花费了多少时间。您必须在该部分开始之前获取一个当前时间,在该部分结束之后获取另一个当前时间。然后取这两次的差。在
ctime
中声明现成函数来完成所有这些工作。只要签出上面的链接。

如果您使用的是GNU gcc/g++:

尝试使用--coverage重新编译,重新运行程序并使用gprof实用程序分析结果文件。它还将打印函数的执行时间


编辑:编译并链接-pg,而不是-coverage,--coverage是用于gcov的(实际执行的行)

std::chrono
boost::chrono
(在编译器不支持C++11的情况下)可用于此目的

std::chrono::high_resolution_clock::time_point start( 
    std::chrono::high_resolution_clock::now() );
....
std::cout << (std::chrono::high_resolution_clock::now() - start);
std::chrono::高分辨率时钟::时间点开始(
std::chrono::高分辨率时钟::now();
....

你需要自己测量时间。我通常使用的小秒表类如下所示:

#include <chrono>
#include <iostream>

template <typename Clock = std::chrono::steady_clock>
class stopwatch
{
    typename Clock::time_point last_;

public:
    stopwatch()
        : last_(Clock::now())
    {}

    void reset()
    {
        *this = stopwatch();
    }

    typename Clock::duration elapsed() const
    {
        return Clock::now() - last_;
    }

    typename Clock::duration tick()
    {
        auto now = Clock::now();
        auto elapsed = now - last_;
        last_ = now;
        return elapsed;
    }
};

template <typename T, typename Rep, typename Period>
T duration_cast(const std::chrono::duration<Rep, Period>& duration)
{
    return duration.count() * static_cast<T>(Period::num) / static_cast<T>(Period::den);
}

int main()
{
    stopwatch<> sw;
    // ...
    std::cout << "Elapsed: " << duration_cast<double>(sw.elapsed()) << '\n';
}
#包括
#包括
模板
阶级秒表
{
typename时钟::时间点最后一次;
公众:
秒表
:最后一次(时钟::现在()
{}
无效重置()
{
*这个=秒表();
}
typename时钟::持续时间经过()常数
{
返回时钟::现在()-最后一次;
}
typename时钟::持续时间刻度()
{
自动现在=时钟::现在();
自动运行=现在-最后一次;
上次=现在;
返回时间已过;
}
};
模板
持续时间(const std::chrono::duration&duration)
{
返回duration.count()*static_cast(Period::num)/static_cast(Period::den);
}
int main()
{
秒表开关;
// ...
std::cout您可以使用标准库中的
标题:

#include <chrono>
#include <iostream>

unsigned long long fib(unsigned long long n) {
    return (0==n || 1==n) ? 1 : fib(n-1) + fib(n-2);
}

int main() {
    unsigned long long n = 0;
    while (true) {
        auto start = std::chrono::high_resolution_clock::now();
        fib(++n);
        auto finish = std::chrono::high_resolution_clock::now();

        auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(finish-start);
        std::cout << microseconds.count() << "µs\n";
        if (microseconds > std::chrono::seconds(1))
            break;
    }
}
#包括
#包括
无符号长fib(无符号长n){
返回值(0==n | | 1==n)?1:fib(n-1)+fib(n-2);
}
int main(){
无符号长n=0;
while(true){
自动启动=标准::时钟::高分辨率时钟::现在();
fib(++n);
自动完成=标准::时钟::高分辨率时钟::现在();
自动微秒=标准::计时::持续时间\u转换(完成-开始);
标准::cout可能重复:

您可以使用time.h C标准库(在中有详细说明)。以下程序可满足您的需要:

#include <iostream>
#include <time.h>

using namespace std;

int main()
{
    clock_t t1,t2;
    t1=clock();
    //code goes here
    t2=clock();
    float diff = ((float)t2-(float)t1)/CLOCKS_PER_SEC;
    cout << "Running time: " << diff << endl;

    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
时钟t1,t2;
t1=时钟();
//代码在这里
t2=时钟();
浮点数差=((浮点数)t2-(浮点数)t1)/每秒时钟数;

无法在语句开始执行之前节省时间,然后让它们执行,再次获取时间并从中减去第一次时间。指向另一个答案的简单链接本身不是答案。请使用“标志”功能来表示重复的问题。@Mat:我不确定mrowa是否有足够的声誉来标记…@Mat:我喜欢信任源代码,这就是为什么我链接到代码片段而不是复制和粘贴-但是,关于可能的重复,你说得很对。(是的,现在标记是从15个代表开始启用的)这是
高分辨率时钟
,而不是
高分辨率计时器
@BenoitBlanchon谢谢你,很抱歉弄错了,我编辑了我的答案。这不好。如果你计算每秒钟的时钟数,那么当时钟速度改变时,你会得到差异。这项技术自2005年以来就存在了。根据维基百科:我不得不对是
int start_s=clock();
// the code you wish to time goes here
int stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;