Why is CLK_TCK undeclared although I included time.h in C++?

520 Views Asked by At


I'm trying to measure the operation time of the function called f1. In this process, I have heard of using CLK_TCK.
I used the web editor ideone.com to run my code.

The code I wrote is shown below:

    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    clock_t start, stop;
    double duration;
    #define MAXN 10
    double f1 (int n, double a[], double x) {
        int i;
        double p=a[0];
        for (i=1; i<=n; i++)
         p+=(a[i]*pow(x,i));
         return p;
        }
    
    int main() {
        int i;
        double a[MAXN];
        for (i=0;i<MAXN;i++) a[i]=(double)i;

        start=clock();
        f1(MAXN-1,a,1.1);
        stop=clock();
        duration=((double)(stop-start))/CLK_TCK;
        printf("ticks1 = %f\n", (double)(stop-start));
        printf("durationa1=%6.2e\n",duration);
        
        return 0;
    }


When I ran the code, however, as shown below, in the output space of the web editor (maybe correspond to terminal in some editors), it appeared the message which said that CLK_TCK is undefined and advised me to use CLOCK_TAI instead.
The sentences shown in the output space in the web editor.

    Compilation error #stdin compilation error #stdout 0s 5532KB
    prog.cpp: In function ‘int main()’:
    prog.cpp:29:34: error: ‘CLK_TCK’ was not declared in this scope
      duration=((double)(stop-start))/CLK_TCK;
                                      ^~~~~~~
    prog.cpp:29:34: note: suggested alternative: ‘CLOCK_TAI’
      duration=((double)(stop-start))/CLK_TCK;
                                      ^~~~~~~
                                      CLOCK_TAI


I am wondering why this error happened and searched for the Internet about both. However, although I found the explanation regarding same situations, such as case1, and case2, and then have some idea about how to solve this problem, I couldn't really understand the reason of this error.
During the search, I found CLK_TCK is included in time.h library. However, I did include this as header so I'm not sure the reason why editor told me CLK_TCK is undeclared.

Question
Would you please tell me why is CLK_TCK undeclared in my case? Thank you.


Notation
I am also wondering why in both case1 and case2, CLK_PER_SEC is suggested to use, but why the error message in my case suggests using CLOCK_TAI.

0

There are 0 best solutions below