函数的运行时间 [英] Running time of functions

查看:468
本文介绍了函数的运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印我的功能的运行时间。由于某种原因,我的计时器总是返回0.任何人都可以告诉我为什么?

  double RunningTime(clock_t time1,clock_t time2)
{
double t = time1 - time2;
double time =(t * 1000)/ CLOCKS_PER_SEC;
return time;
}

int main()
{
clock_t start_time = clock();


//一些代码.....


clock_t end_time = clock();

std :: cout<< 经过时间:< double(RunningTime(end_time,start_time))< 女士;

return 0;
}

我试图使用 gettimeofday 并返回0。

  double get_time()
{
struct timeval t;
gettimeofday(& t,NULL);
double d = t.tv_sec +(double)t.tv_usec / 100000;
return d;
}

int main()
{
double time_start = get_time();

//一些代码......

double time_end = get_time();

std :: cout<< time_end - time_start;

return 0;
}

也尝试使用 chrono 它给我各种构建错误:




  • 错误:#error此文件需要编译器和库支持
    即将到来的ISO C ++标准,C ++ 0x。此支持目前为

    实验性的,必须使用-std = c ++ 0x或-std = gnu ++ 0x
    编译器选项启用。

  • warning:'auto'将改变C ++ 0x中的含义;请删除它

  • 错误:ISO C ++禁止宣布't1'没有类型错误:
    'std :: chrono'尚未声明

  • 错误:请求成员'count'在'(t2 - t1)',这是
    非类型'int'



    int main()
    {
    auto t1 = std :: chrono :: high_resolution_clock :: now();

      //有些代码...... 

    auto t2 = std :: chrono :: high_resolution_clock :: now();

    std :: cout<< 经过时间:< std :: chrono :: duration_cast< std :: chrono :: milliseconds>(t2-t1).count()< milliseconds\\\
    ;

    return 0;
    }



解决方案

计时器滴答大约等于1 / CLOCKS_PER_SEC秒,这是一个毫秒的分辨率。要查看一个实数(非零)数字,您应该调用一个很长时间的函数,或者使用另一个具有更高时间分辨率的库:




  • new c ++ 11x library chrono (使用MSVS 2012)

  • boost :: chrono (不幸的是,该库指的是很多其他人)

  • POSIX函数 gettimeofday ,可提供1微秒的时间解决方案


    • I am wanting to print the running time of my functions. For some reason my timer always returns 0. Can anyone tell me why?

      double RunningTime(clock_t time1, clock_t time2)
      {
          double t=time1 - time2;
          double time = (t*1000)/CLOCKS_PER_SEC;
          return time;
      }
      
      int main()
      {
           clock_t start_time = clock();
      
      
           // some code.....
      
      
          clock_t end_time = clock();
      
          std::cout << "Time elapsed: " << double(RunningTime(end_time, start_time)) << " ms";
      
          return 0;
      }
      

      I attempted to use gettimeofday and it still returned 0.

      double get_time()
      {
          struct timeval t;
          gettimeofday(&t, NULL);
          double d = t.tv_sec + (double) t.tv_usec/100000;
          return d;
      }
      
      int main()
      {
              double time_start = get_time();
      
              //Some code......
      
              double time_end = get_time();
      
              std::cout << time_end - time_start;
      
          return 0;
      }
      

      Also tried using chrono and it gave me all kinds of build errors:

      • error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently
        experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
      • warning: 'auto' will change meaning in C++0x; please remove it
      • error: ISO C++ forbids declaration of 't1' with no type error: 'std::chrono' has not been declared
      • error: request for member 'count' in '(t2 - t1)', which is of non-class type 'int'

        int main() { auto t1 = std::chrono::high_resolution_clock::now();

                    //Some code......
        
                    auto t2 = std::chrono::high_resolution_clock::now();
        
                    std::cout << "Time elapsed: " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << " milliseconds\n";
        
                return 0;
            }
        

      解决方案

      A timer tick is approximately equal to 1/CLOCKS_PER_SEC second, which is a millisecond resolution. To see a real (non-zero) number, you should either invoke a very long-time function or use another library with a higher time resolution facility:

      • new c++11x library chrono (use MSVS 2012)
      • boost::chrono (unfortunately, the library refers to a lot of others)
      • POSIX function gettimeofday, which gives you a 1 microsecond time resolution

      这篇关于函数的运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆