How to calculate compilation time using c?

is there function with library "time.h" to calculate the processing time of the code?

As in, can you find out inside the code itself how long the same code to compile?

Or do you just want to time shell commands like cc externally?

no, i just want to measure the performance of my code "using c"

so i want how much time it take to process my code
i used that

#include <time.h>
     
     clock_t start, end;
     double cpu_time_used;
     
     start = clock();
     ... /* Do the work. */
     end = clock();
     cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

it was so weird since i printed cpu_time_used
like that

printf(cpu_time_used)

it gave me error

printf("%f",cpu_time_used)

gave me 0.0000000000 which is wrong

printf("%i",cpu_time_used)

gave me 176543867 which is integer that print double make no since O.o

%i and %d mean integer, float or double would be %f.

I usually use this code:

#include <sys/time.h>
#include <sys/types.h>
#include <stdio.h>

/* returns a timestamp in microseconds */
int64_t micro(void)
{
	int64_t val;
	struct timeval tv;
	gettimeofday(&tv, NULL);
	val=tv.tv_sec;
	val*=1000000;
	val+=tv.tv_usec;
	return(val);
}
1 Like