【问题标题】:Calling a function for a period of time调用一个函数一段时间
【发布时间】:2011-04-24 04:58:05
【问题描述】:

我想进行调用 - 函数调用或在一段时间内执行某些条件...通常为 10 到 20 秒。

我会在一段时间内获得一些用户输入并这样做......

在 Linux/Unix 系统上使用的正确函数是什么?

gettimeofday 似乎是要走的路......或者 time_t time(time_t *t) ... 似乎很简单。什么是首选?

【问题讨论】:

  • 我不明白你在问什么。您希望函数调用持续 10-20 秒,还是要休眠 10-20 秒然后调用它?函数调用持续多长时间几乎取决于函数本身。
  • 我想调用一个函数大约 10 到 20 秒......或者真的调用任何东西......问题是我如何最好地测量时间......有很多重叠的 Unix/Posix时间函数。
  • 你想调用这个函数的次数有限制吗?一度?数百万次?如果是一次,您可能需要考虑注册一个 20 秒的定时器预设。

标签: c++ c linux time timestamp


【解决方案1】:

这样就可以了

#include <ctime>
/*
your function here
*/

    int main()
    {
        double TimeToRunInSecs = ...;
        clock_t c = clock();
        while(double(clock()-c)/CLOCKS_PER_SEC < TimeToRunInSecs)
       {
          myFunc();
       }
    }

标准clock() 函数从进程开始返回SOMETHING 的数量。一秒钟内有 CLOCK_PER_SEC 一些东西:)

HTH

【讨论】:

  • 这几乎肯定不是 OP 想要的。
  • @Amigable:你为什么这么认为?我想这正是他想要的。
  • CPU时间很大程度上取决于CPU性能、负载和很多因素。我可能错了,但无论如何我认为这个问题需要解决。
【解决方案2】:

我可以做一个

time_t current_time = time(0);

并衡量它...但是有没有首选的方法...主要这是一个最佳实践类型的问题...。

x

【讨论】:

    【解决方案3】:

    所以你想要这样的东西吗?这将在接下来的 20 秒内重复调用 myfunc()。所以可以进行 1 次调用(如果 myfunc 至少需要 20 秒才能运行)或数百次调用(myfunc() 需要几毫秒才能完成):

    #include <time.h>
    
    void myfunc()
    {
        /* do something */
    }    
    
    int main()
    {
        time_t start = time(NULL);
        time_t now = time(NULL);
        while ((now - start) <= 20) {
            myfunc();
            time_t now = time(NULL);
        }
    }
    

    也许值得问问你最终想要实现什么。如果这是用于分析(例如,函数 f 执行的平均时间量是多少),那么您可能想要查看其他解决方案 - 例如,使用 gcc 为您提供的内置分析(当使用“ -pg" 选项),并使用 gprof 进行分析。

    【讨论】:

    • 对于表述不当的问题的最佳答案,+1。
    【解决方案4】:

    几件事..

    如果您想确保函数花费时间 X 来完成,无论函数中的实际代码花费多长时间,请执行以下操作(高度伪代码)

    class Delay
    {
      public:
        Delay(long long delay) : _delay(delay) // in microseconds
        {
           ::gettimeofday(_start, NULL); // grab the start time...
        }
    
        ~Delay()
        {
          struct timeval end;
    
          ::gettimeofday(end, NULL); // grab the end time
          long long ts = _start.tv_sec * 1000000 + _start.tv_usec;
          long long tse = end.tv_sec * 1000000 + end.tv_usec;
    
          long long diff = tse - ts;
          if (diff < _delay)
          {
            // need to sleep for the difference...
            // do this using select;
            // construct a struct timeval (same as required for gettimeofday)
            fd_set rfds;
            struct timeval tv;
            int retval;
    
            FD_ZERO(&rfds);
    
            diff = _delay - diff; // calculate the time to sleep
    
            tv.tv_sec = diff / 1000000;
            tv.tv_usec = diff % 1000000;
    
            retval = select(0, &rfds, NULL, NULL, &tv);
            // should only get here when this times out...
          }
        }
      private:
        struct timeval _start;   
    };
    

    然后在你的函数顶部定义一个延迟类的实例来延迟 - 应该可以解决问题......(这段代码未经测试,可能有错误,我只是输入它来给你一个想法...... )

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2018-06-07
      • 2021-08-07
      • 2011-04-12
      • 2016-11-03
      • 1970-01-01
      相关资源
      最近更新 更多