advances issues how to time c code debugging dynamic allocation of matrices

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

Upload: spencer-black

Post on 18-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

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.

TRANSCRIPT

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

ADVANCES ADVANCES ISSUESISSUES

How to time C Code How to time C Code

DebuggingDebugging

Dynamic Allocation of Dynamic Allocation of MatricesMatrices

Page 2: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

HOW to time C CodeHOW 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*/

Page 3: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

HOW to time C Code : TypesHOW 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. 

Page 4: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

HOW to time C Code : HOW to time C Code : FunctionsFunctions

• 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

Page 5: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

HOW to time C Code : HOW to time C Code : FunctionsFunctions

• 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.

Page 6: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

HOW to time C Code : Example1HOW to time C Code : Example1#include<time.h>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);

}

Page 7: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

HOW to time C Code : Example2HOW to time C Code : Example2#include <time.h>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 differenceprintf("Difference

%lf\n",difftime(t4,t3));

printf("Date %s",ctime(&t3));

}

/* output : Difference 0.000000Date Mon May 11 10:40:39 2009 */

Page 8: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

DebuggingDebugging• 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

Page 9: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

DebuggingDebugging• 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

Page 10: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

Dynamic Allocation of Dynamic Allocation of MatricesMatrices

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

Page 11: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

Dynamic Allocation of Dynamic Allocation of MatricesMatrices

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.

Page 12: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

Dynamic Allocation of Dynamic Allocation of MatricesMatrices

#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);

Page 13: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

Deallocate Dynamic Deallocate Dynamic MatricesMatrices

for(i=0; i<rowN; i++)free(x[i]);

free(x);

Page 14: ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices

DYnamic Matrices : ExampleDYnamic Matrices : Example#include <stdio.h>

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