Presentation is loading. Please wait.

Presentation is loading. Please wait.

ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices.

Similar presentations


Presentation on theme: "ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices."— Presentation transcript:

1 ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

2 HOW to time C Code “time.h” contains prototypes of some functions that access machine’s internal clock and type defitions time_t and clock_t –typedef long clock_t; –typedef long time_t; –clock_t clock(void); –time_t time(time_t *p); –double difftime(time_t time1, time_t time0); –#define CLOCKS_PER_SECOND 60 /*machine-dependent*/

3 HOW to time C Code : Types clock_t : Type capable of representing clock tick counts and support arithmetical operations time_t : Type capable of representing times and support arithmetical operations. It is almost universally expected to be an integral value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970.

4 HOW to time C Code : Functions clock_t clock(void); When a program is executed, the operating system keeps track of the processor time that is being used. When clock() is invoked, the value returned is the system's best approximation to the time used by the program up to that point

5 HOW to time C Code : Functions time_t time(time_t *p); The function time() typically returns the number of seconds that have elapsed since 1 January 1970. This function can be used to seed random number generator -- > srand(time(NULL)) double difftime(time_t time1, time_t time0); If two values of time() are passed to difftime() the difference expressed inseconds is returned as a double.

6 HOW to time C Code : Example1 #include int main(void) {int i=0; // Declare clock variables: clock_t t1, t2; float ratio; ratio = 1./CLOCKS_PER_SEC; // Record the start time: t1 = clock(); printf("%f\n",t1*ratio); /**Run a segment of Code **/ for(i=0;i<10000000;i++); // Record the end time: t2 = clock(); printf("%f\n",t2*ratio); // Print out the difference, converting // it to seconds: printf("Time = %f", ratio*t2 - ratio*t1); }

7 HOW to time C Code : Example2 #include int main(void) {int i=0; // Declare time variables: time_t t3,t4; // Record the start time: time(&t3); /************************* Run a segment of Code *************************/ for(i=0;i<100000000;i++); // Record the end time: time(&t4); // Print out the difference printf("Difference %lf\n",difftime(t4,t3)); printf("Date %s",ctime(&t3)); } /* output : Difference 0.000000 Date Mon May 11 10:40:39 2009 */

8 Debugging A methodical process of finding and reducing the number of bugs, or defects, in a computer or a piece of electronic hardware thus making it behave as expected( definition from wikipedia ) Debugger is the tool to debug your program

9 Debugging A debugger allows the programmer to step through the code a line at a time and to see the values of variables and expressions at each step. –example: gdb (The GNU debugger) –for MS Visual Studio debugger: Start Debug Step Into / Step Over

10 Dynamic Allocation of Matrices In mathematics, a matrix (plural matrices, or less commonly matrixes) is a rectangular array of numbers, as shown at the right (definition from wikipedia)

11 Dynamic Allocation of Matrices int x [3] is similar to int *x=malloc( sizeof(int)*3) int x [3][3] is similar to int **x How to allocate memory for a dynamic matrice?? 1. Allocate memory for rows. 2. Allocate memory for columns.

12 Dynamic Allocation of Matrices #define rowN 5; #define columnN 6; int **x // create rowN X columnM matrice // 1. Allocate memory for rows x =(int **) malloc( sizeof(int *)*rowN); // 2. Allocate memory for columns for(i=0;i<rowN;i++) x[i]=(int *)malloc(sizeof(int)*columnN);

13 Deallocate Dynamic Matrices for(i=0; i<rowN; i++) free(x[i]); free(x);

14 DYnamic Matrices : Example #include int main(void) { int **x; int rowN,columnN, i,j; printf("Give values for row and column"); scanf("%d%d",&rowN,&columnN); x = (int **)calloc(rowN,sizeof(int*)); for(i=0;i<rowN;i++) x[i]=(int *)malloc(sizeof(int)*columnN); for(i=0;i<rowN;i++) for(j=0;j<columnN;j++) x[i][j]=i*j; for(i=0;i<rowN;i++) {for(j=0;j<columnN;j++) printf("%d ", x[i][j]); printf("\n"); } for(i=0; i<rowN; i++) free(x[i]); free(x); return 0; }


Download ppt "ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices."

Similar presentations


Ads by Google