9

How do I time my C program in Clion? Is there an option I can use to to tell me how long long my program took to run?

something like

> finished in .003 seconds

here is what I am referring to when I say debugger/console: enter image description here


conor
  • 1,131
  • 1
  • 15
  • 20

1 Answers1

3

You can measure the time of your code from inside your C program.

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

int main() {
    clock_t start = clock();

    int n = 100; 
    int i=0;
    while(i<100*n)
        i++;

    clock_t stop = clock();
    double elapsed = (double) (stop - start) / CLOCKS_PER_SEC;
    printf("\nTime elapsed: %.5f\n", elapsed);
    return 0;
}
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424