Conor Cosnett Conor Cosnett - 2 months ago 22
C Question

How do I get the CLion debugger/console to tell me how many seconds my program took to run?

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



Answer

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;
}