Check how much time your Processor takes to execute your C-Program Code
#include<stdio.h>
#include<conio.h>
#include <time.h>
#include <stdlib.h>
clock_t start, stop;
#define START if ( (start = clock()) == -1) {printf("Unknown Error! Cannot call clock");exit(1);}
#define STOP if ( (stop = clock()) == -1) {printf("Unknown Error! Cannot call clock");exit(1);}
#define PRINT_TIME printf( "%10.9f seconds used by the processor to execute your code", ((double)stop-start)/CLOCKS_PER_SEC);
int main()
{
clrscr();
START;
// put your code here whose time of execution u want to know
STOP;
PRINT_TIME;
getch();
return 0;
}
For Example: The following "while loop inside the program takes 5 sec (approx.) to execute" because sleep(1), temporarily stops the program for each 1 sec for each execution of loop.
#include<stdio.h>
#include<conio.h>
#include <time.h>
#include <stdlib.h>
clock_t start, stop;
#define START if ( (start = clock()) == -1) {printf("Unknown Error! Cannot call clock");exit(1);}
#define STOP if ( (stop = clock()) == -1) {printf("Unknown Error! Cannot call clock");exit(1);}
#define PRINT_TIME printf( "%10.9f seconds used by the processor to execute your code", ((double)stop-start)/CLOCKS_PER_SEC);
int main()
{
int i=0;
clrscr();
START;
// put ur code here whose time of execution u want to know
while(i<5)
{
i++;
sleep(1);
printf("\n\n");
STOP;
PRINT_TIME;
}
STOP;
PRINT_TIME;
getch();
return 0;
}
No comments:
Post a Comment