简体   繁体   English

ctime无法正确获取循环开始/结束的时差

[英]ctime not getting time difference of start/end of loop correctly

I'm trying to have a loop execute for an exact time specified by executionTime . 我正在尝试执行一个由executionTime指定的确切时间的循环。 I am using ctime to get this timing, and I need it to be within a few milliseconds of accuracy (which I believe it is). 我正在使用ctime来获取此时间,我需要它在几毫秒的精度范围内(我相信是这样)。 Once that loop runs for the execution time specified, it should break. 一旦该循环运行了指定的执行时间,它就会中断。

My problem is for the execution time of 0.5, the result being printing is 1. Upon further testing it appears my program rounds up the execution time to the nearest integer. 我的问题是执行时间为0.5,正在打印的结果为1。经过进一步测试,看来我的程序将执行时间四舍五入为最接近的整数。 So for 4.5 executionTime , the execution time being printed is 5.0000000. 因此,对于4.5 executionTime ,正在打印的执行时间是5.0000000。 My code is below. 我的代码如下。 I am not sure where this error is coming from. 我不确定此错误来自何处。

#include <time.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 

int main() 
{
    float  executionTime = 4.5; 
    int i;
    float  timeDifference = -1;  
    time_t t1;
    time_t t2;

    t1 = time(0);

    for (i = 0; i < 1000000000; i++) 
    {
        t2 = time(0); 

        timeDifference = difftime(t2, t1); 

        if (timeDifference >= executionTime) 
        { 
            break; 
        }

    }

    printf("time difference is: %f\n", timeDifference); 

    return 0; 

}

NEW VERSION trying to use clock_gettime. 尝试使用clock_gettime的新版本。 This version has the issue of never breaking when execution time is reached for some reason inside the loop. 这个版本的问题是循环内由于某种原因达到执行时间时永不中断。

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define BILLION 1E9 

int main()
{
    float  executionTime = 4.5;
    int i;
    float  elapsedTime = -1;

    struct timespec start;
    struct timespec stop;

    //Get starting time 
    clock_gettime(CLOCK_REALTIME, &start);

    for (i = 0; i < 1000000000; i++)
    {
        //Get current time
        clock_gettime(CLOCK_REALTIME, &stop);

        elapsedTime = ((stop.tv_sec - start.tv_sec) + (stop.tv_nsec - start.tv_nsec)) / BILLION;

        if (elapsedTime >= executionTime)
        {
            break;
        }
    }

    printf("time difference is: %f\n", elapsedTime);

    return 0;

}

time() returns to the nearest second and difftime() returns the difference of those. time()返回最接近的difftime()difftime()返回两者之间的差。 So basically this function compute difference of to integers. 因此,基本上该函数计算到整数的差。 For a more accurate estimation you can use clock() 为了获得更准确的估计,您可以使用clock()

time_t purpose is for measuring calendar times time_t目的是用于测量日历时间

int main() 
{
    float  executionTime = 1.3; 
    int i;
    double  timeDifference = 1.0;  
    clock_t t1;
    clock_t t2;

    t1 = clock();

    for (i = 0; i < 1000000000; i++) 
    {
        timeDifference = (double)(clock() - t1) / CLOCKS_PER_SEC;

        if (timeDifference >= executionTime) 
        { 
            break; 
        }

    }

    printf("time difference is: %.9g\n", timeDifference); 

    return 0; 

}

clock_gettime can be used to get greater accuracy. clock_gettime可用于获得更高的准确性。
Call delay with a value greater than 0 to set the delay time, then with -1 to check the elapsed time. 呼叫delay一个大于0的值来设定延迟时间,然后用-1检查所经过的时间。
Calling clock_gettime in main will give the elapsed time in main. 在main中调用clock_gettime将给出main中的经过时间。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#define BILLION  1E9

int delay ( double seconds)
{
    static double usec = 0.0;
    double elapsed = 0.0;
    static struct timespec start;
    struct timespec stop;

    if ( 0.0 <= seconds) {
        usec = seconds;
        clock_gettime ( CLOCK_REALTIME, &start);
        return 0;
    }
    clock_gettime ( CLOCK_REALTIME, &stop);
    elapsed = difftime ( stop.tv_sec, start.tv_sec);
    elapsed += ( stop.tv_nsec - start.tv_nsec) / BILLION;
    if ( ( elapsed < usec)) {
        return 1;
    }
    return 0;
}

int main() {

    double  executionTime = 4.5;
    int i;
    double  timeDifference = -1;
    struct timespec end;
    struct timespec begin;

    clock_gettime ( CLOCK_REALTIME, &begin);
    delay( executionTime);//call to set time

    for (i = 0; i < 1000000000; i++)
    {
        if ( !delay( -1)) {//call to check elapsed time
            break;
        }
    }
    clock_gettime ( CLOCK_REALTIME, &end);
    timeDifference = difftime ( end.tv_sec, begin.tv_sec);
    timeDifference += ( end.tv_nsec - begin.tv_nsec) / BILLION;

    printf("time difference is: %f\n", timeDifference);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM