Pigna
Pigna

Reputation: 2924

How to time a C program

I've read this post here and I followed the instructions, applying them to a simple program that sums all the numbers below 1000 divisible by 3 and 5.

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

clock_t begin, end;
double time_spent;

begin = clock();
int sumDivisibleBy (div, limit) {
    int h = (limit - 1)/div;
    return div*h*(h+1)/2;
}

int main(void) {
    int l = 1000;
    int s = sumDivisibleBy(3,l) + sumDivisibleBy(5,l) - sumDivisibleBy(15,l);
    printf("%d \n", s);
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC
printf("%f \n", time_spent)

Now, when I type in the terminal "make 1" (the file is called 1.c), this is what I get:

cc     1.c   -o 1
1.c:9:1: warning: data definition has no type or storage class [enabled by default]
 begin = clock();
 ^
1.c:9:1: error: conflicting types for ‘begin’
1.c:6:9: note: previous declaration of ‘begin’ was here
 clock_t begin, end;
         ^
1.c:9:1: error: initializer element is not constant
 begin = clock();
 ^
1.c:20:1: warning: data definition has no type or storage class [enabled by default]
 end = clock();
 ^
1.c:20:1: error: conflicting types for ‘end’
1.c:6:16: note: previous declaration of ‘end’ was here
 clock_t begin, end;
                ^
1.c:20:1: error: initializer element is not constant
 end = clock();
 ^
1.c:21:1: warning: data definition has no type or storage class [enabled by default]
 time_spent = (double)(end - begin) / CLOCKS_PER_SEC
 ^
1.c:21:1: error: conflicting types for ‘time_spent’
1.c:7:8: note: previous declaration of ‘time_spent’ was here
 double time_spent;
        ^
1.c:21:1: error: initializer element is not constant
 time_spent = (double)(end - begin) / CLOCKS_PER_SEC
 ^
1.c:21:1: error: expected ‘,’ or ‘;’ at end of input
make: *** [1] Error 1

Why is it? Can somebody help please?

Upvotes: 2

Views: 762

Answers (2)

Matthew Gunn
Matthew Gunn

Reputation: 4519

While you can initialize global variables outside of a code block, you can't do what you're doing (if you want code to work). In general, code shouldn't be sitting outside of functions. You want end = clock() to be executed at the end! To do this, it needs to be at the end of the main() function.

Move the code into main() so it reads:

int main(void) {
    begin = clock();

    ... //your code here

    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("time spent %f \n", time_spent);
}

Upvotes: 1

Jens
Jens

Reputation: 72649

You can't use function calls to initialize file scope variables (those outside main).

You must move begin and end into main() (well, at least their initialization).

A C program doesn't execute top to bottom; it starts with main(). The values of initialized file scope variables must be known at compile-time, which is the reason you can't use function calls.

To get meaningful results, I also suggest you run the code to be tested many times in a loop, since the resolution of the clock is often too coarse to time a few instructions, i.e. do something like

 begin = ...
 for (j = 0; j < 10000; ++j) {
     code_to_test();
 }
 end = ...

Upvotes: 3

Related Questions