C Program to calculate the total execution time of a program

Levels of difficulty: / perform operation:

Here we used the “time.h” preprocessor. In this program we used the clock_t variables start and end , They starts the time counter and ends the counter. Execution time of a program is useful to calculate the efficiency of the program.

C Program

int main() {
	int i;
	double total_time;
	clock_t start, end;
	start = clock();
	//time count starts 
	for (i = 0; i < 25000; i++) {
		printf("random_number[%d]= %d\n", i + 1, rand());
	end = clock();
	//time count stops 
	total_time = ((double) (end - start)) / CLK_TCK;
	//calulate total time
	printf("\nTime taken to print 25000 random number is: %f", total_time);
	return 0;