How to measure code execution time (GCC) in C

1 Answer

0 votes
#include <stdio.h>
#include <time.h>
 
int main() {
    clock_t begin = clock();
 
    long sum = 0;
    for (int i = 0; i < 10000000; i++) {
        sum += i;
    }
    printf("sum = %ld\n", sum);
 
    clock_t end = clock();
    double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("%f sec", time_spent);
 
    return 0;
}
 
 
 
 
/*
run:
 
sum = 49999995000000
0.030671 sec
 
*/

 



answered Apr 5, 2019 by avibootz
edited May 29, 2023 by avibootz

Related questions

1 answer 158 views
158 views asked May 29, 2023 by avibootz
1 answer 312 views
2 answers 268 views
2 answers 291 views
2 answers 175 views
1 answer 153 views
153 views asked Sep 26, 2021 by avibootz
...