How to calculate the code execution time in C

1 Answer

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

int main(void) {
   
    clock_t begin = clock();
    
    long sum = 0;
    for (long i = 0; i < 1000000; i++) {
            sum += i;
    }

    clock_t end = clock();
    double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    
    printf("%f seconds", time_spent);

    return 0;
}




 
/*
run:
 
0.003128 seconds
 
*/

 



answered Sep 26, 2021 by avibootz

Related questions

1 answer 370 views
2 answers 285 views
3 answers 369 views
1 answer 418 views
2 answers 408 views
2 answers 432 views
1 answer 301 views
...