如何定义一个循环运行几秒/分钟

Ang*_*ngs 6 c linux time clock time-t

我的目的是在while规定的时间内执行循环(例如,本例中为90秒).它不一定是90秒,但1-2秒的不准确是可以接受的.为了这个目的,我试图使用clock()`函数:

int main(void){
      clock_t start, end;
      volatile double elapsed;
      start = clock();
      int terminate = 1;
      while(terminate)
      {
              end = clock();
              elapsed = ((double) (end-start)) / (double) CLOCKS_PER_SEC *1000;
              printf("elapsed time:%f\n",elapsed);
              if(elapsed >= 90.0)
                        terminate = 0;
               usleep(50000);
       }
      printf("done..\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我在我的笔记本电脑上运行它(x86,3.13内核,gcc 4.8.2)时,我的秒表测量它完成72秒.(1000 elapsed我的笔记本电脑必须具有几秒钟的准确度)

当我在ARM设备(armv5tejl,3.12内核,gcc 4.6.3)上运行它时,需要58秒才能完成代码.(我需要使用100elapsed对的ARMv5).

我在室温下运行代码,因此时钟应该稳定.我知道内核会休眠线程,并且有时间不准确地唤醒它们等等.因此,正如我之前所说,我不希望得到一个完美的时序,但它应该有一些准确性.

我曾试图只使用usleep(偶数nanosleep),但分辨率也不好.最后,我想出了获取系统时间(小时,分钟,秒)的底部代码,然后计算经过的时间.并且它具有良好的准确性.

我想知道是否有另一种解决方案使用成本更低?

typedef struct{
    int hour;
    int minute;
    int second;
} timeInfo;

timeInfo getTimeInfo(void){
    timeInfo value2return;
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    value2return.hour = timeinfo->tm_hour;
    value2return.minute = timeinfo->tm_min;
    value2return.second = timeinfo->tm_sec;
    return value2return;
}

int checkElapsedTime(const timeInfo *Start, const timeInfo *Stop, const int Reference){
    if(Stop->hour < Start->hour){
        printf("1:%d\n", (Stop->hour +24) *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second));
         if( ( (Stop->hour +24) *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second)) >= Reference )
            return 0; //while(0): terminate the loop
         else
             return 1; //while(1)
    }else{
        printf("2:%d\n",Stop->hour *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second));
        if( (Stop->hour *3600 + Stop->minute *60 + Stop->second - (Start->hour *3600 +Start->minute * 60 + Start->second)) >= Reference )
            return 0;
        else
            return 1;
    }
}

int main(void){

    timeInfo stop, start = getTimeInfo();
    int terminate = 1;
    while(terminate)
    {
        stop = getTimeInfo();
        terminate = checkElapsedTime(&start, &stop, 90);
        usleep(5000); //to decrease the CPU load
    }

    printf("terminated\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

最后,我需要在一个内部运行它pthread.

chu*_*ica 6

  1. 使用time()clock().编码目标是确定经过的时间而不是使用的处理器时间.

    当前代码计算过程经过的时间*1000并将其与90秒进行比较.

    clock()@uesp暗示,返回"时钟函数决定使用的处理器时间".C11dr§7.27.2.12.

    time() 返回"时间函数确定当前日历时间"§7.27.2.42

    difftime()找到2之间的差异time_t(无论它们是什么单位/类型)并以秒为单位返回差异.

    int main(void) {
       time_t start, end;
       double elapsed;  // seconds
       start = time(NULL);
       int terminate = 1;
       while (terminate) {
         end = time(NULL);
         elapsed = difftime(end, start);
         if (elapsed >= 90.0 /* seconds */)
           terminate = 0;
         else  // No need to sleep when 90.0 seconds elapsed.
           usleep(50000);
       }
       printf("done..\n");
       return 0;
     }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 轻微:注意:使用时clock(),不需要* 1000.在运行gcc的基于Windows的机器上,对我来说,clock()还返回了调用进程CPU时间.

    elapsed = ((double) (end-start)) / (double) CLOCKS_PER_SEC *1000;
    elapsed = ((double) (end-start)) / CLOCKS_PER_SEC;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 轻微:没必要volatile. elapsed只是因为这段代码而改变了.

    // volatile double elapsed;
    double elapsed;
    
    Run Code Online (Sandbox Code Playgroud)