简体   繁体   English

Linux中的clock_gettime

[英]clock_gettime in Linux

MAIN.c : MAIN.c:

int sum()
{
    int x = 100, y =200, z;
    z =x;
    z=y;
    printf("value is %d", z);
    return 1;

}

int main()
{
    double starttime, stoptime;
    int a=1, b=10, c;
    starttime = GetTimeStamp();          // I am calculating time here.
    printf("start  time is %f", starttime);
    c =a;
    printf("value is %d", c);
    c =b;
    printf("value is %d", c);
    c= a+b;

        printf("value is %d", c);
        printf("value is %d", c);
        printf("value is %d", c);
        printf("value is %d", c);    

    stoptime =GetTimeStamp();    // I want to know the time here.
    printf("stop  time is %f", stoptime);
    return 0;

}

TIMER.c : TIMER.c:

#define BILLION  1000000L
typedef unsigned int uint32_t;

int GetTimeStamp()
{
struct timespec start;

//double startTime;

           // if( (startTime = clock_gettime( CLOCK_REALTIME, &start)) == -1 )
            if((clock_gettime( CLOCK_REALTIME, &start)) == -1 )
            {
              perror("clock gettime");

            }
         //  startTime =start.tv_sec + 0.0000001 * start.tv_nsec;  // to make it milli 


        return ((start.tv_sec + 0.0000001 * start.tv_nsec)/(double)BILLION);
}

timer.h : timer.h:

#ifndef TIMESTAMP_H_
#define TIMESTAMP_H_
int GetTimeStamp();

#endif /* TIMESTAMP_H_ */

I want to create a free running timer and want to get the time when it start executing and also the time when it finish the execution. 我想创建一个自由运行的计时器,并希望获得开始执行的时间以及完成执行的时间。 So I created a timer as above in TIMER.c and I am using that in main.c. 因此,我如上所述在TIMER.c中创建了一个计时器,并在main.c中使用了它。 I am calling the GetTimeStamp() in MAIN.c at the begging of the program and again at the end of time. 我在程序开始时在MAIN.c中调用GetTimeStamp(),并在时间结束时再次调用。 The output : it is showing the same time for both starttime and stop time as 1594.0000000 输出:显示的开始时间和停止时间均为1594.0000000

Your multiplier for tv_nsec is wrong, and your constant BILLION is wrong too. 您的tv_nsec乘数是错误的,而常量BILLION也是错误的。 The prefix n (nano) means 10^-9, and a billion is equal to 10^9. 前缀n (nano)表示10 ^ -9,十亿等于10 ^ 9。 Change: 更改:

    return ((start.tv_sec + 0.0000001 * start.tv_nsec)/(double)BILLION);

to: 至:

    return 1e9 * start.tv_sec + start.tv_nsec;        // return ns time stamp

or: 要么:

    return 1e6 * start.tv_sec + start.tv_nsec * 1e-3; // return µs time stamp

or: 要么:

    return 1e3 * start.tv_sec + start.tv_nsec * 1e-6; // return ms time stamp

or: 要么:

    return start.tv_sec + start.tv_nsec * 1e-9;       // return seconds time stamp

depending on what units you want to use for your timestamp. 取决于您要用于时间戳记的单位。

As already pointed out elsewhere, you might want to use eg a double for your timestamp, rather than an int , since this gives you better range and resolution and it's also what you're already using in main . 正如在其他地方已经指出的那样,您可能希望在时间戳记中使用例如double而不是int ,因为这可以为您提供更好的范围和分辨率,这也是您已经在main使用的。

double GetTimeStamp(void)
{
    ...

    return 1e9 * start.tv_sec + start.tv_nsec;        // return ns time stamp
}

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

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