Michael Vaysman
Michael Vaysman

Reputation: 287

Measureing execution time with C in Windows

I want to measure the execution time of peace of code.
I'm doing it with clock() function.
Here the example:

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

int main(void) {
    clock_t begin=0, end=0;

    begin = clock();
    printf("hello world!!\n");
    end = clock();

    printf("Begin %E\n",begin);
    printf("End %E\n",end);
    printf("Clocks %E\n",end-begin);
    printf("Clocks per second %E\n", CLOCKS_PER_SEC);
    printf("Time %E\n", (end-begin)/CLOCKS_PER_SEC);

    return 0;
}

Here the output:

hello world!!
Begin 2.529616E-320
End 2.535545E-320
Clocks 5.928788E-323
Clocks per second 4.940656E-318
Time 0.000000E+00

Time is 0!
What I'm doing wrong? Is the output correct?

Thanks a lot

Upvotes: 1

Views: 277

Answers (1)

mksteve
mksteve

Reputation: 13073

%E needs floating point (float or double). You are sending integer values

To divide ensure number is floating

(end-begin+0.0)/CLOCKS_PER_SEC);

Upvotes: 6

Related Questions