Saturday 5 May 2012

Calculate Time of execution of C-Program

 

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

Thursday 3 May 2012

Q.  How to print current date in C?
    #include<stdio.h>
    int main()
    {
       printf("Current Date is =>  %s", __DATE__);  // remember its double underscore in DATE
       getch();
       return 0;
   }

 Also,
  #include<stdio.h>
    int main()
    {
       struct current_date d;
       getdate(&d);
       printf("Current Date is =>  %d-%d-%d",d.da_day, d.da_mon, d.da_year);
       getch();
       return 0;

   }