0


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.

XYJ
  • 11
  • 5
  • Include [mre] here as instead of external links. It should focus on one problem only. – Allan Wind Sep 06 '22 at 03:38
  • Please provide the code in a code block. While there are plenty of people who would be happy to help you, none of them want to transcode your photo into text in order to see what's happening. – Layne Bernardo Sep 06 '22 at 03:38
  • 1
    Also, `CLK_TCK` is deprecated and probably has just been removed from your system version of `time.h`. Probably you should use `CLOCKS_PER_SEC` instead. – Layne Bernardo Sep 06 '22 at 03:39
  • You can share code directly from ideone by using the full link shown in the URL. Even better, you can post your code as text in your question. – paddy Sep 06 '22 at 04:09
  • Thank you for your answers. I posted the code I wrote in my question. – XYJ Sep 06 '22 at 04:16

0 Answers0