26 - Code Optimization 1 Outline Codemotion Memoryoptimization Suggestedreading 5.3~5.6 2 Motivation easilysee10: algorit

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Code Optimization
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline Machine-Independent Optimization Code motion Memory optimization Suggested reading 5.3 ~ 5.6
Background image of page 2
3 Motivation Constant factors matter too! easily see 10:1 performance range depending on how code is written must optimize at multiple levels algorithm, data representations, procedures, and loops
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Motivation Must understand system to optimize performance how programs are compiled and executed how to measure program performance and identify bottlenecks how to improve performance without destroying code modularity and  generality
Background image of page 4
5 Vector ADT typedef struct { int len ; data_t *data ; } vec_rec, *vec_ptr ;   typedef int data_t ; length data 0 1 2 length–1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Procedures vec_ptr new_vec(int len) Create vector of specified length data_t *get_vec_start(vec_ptr v) Return pointer to start of vector data
Background image of page 6
7 Procedures int get_vec_element(vec_ptr v, int index, data_t *dest) Retrieve vector element, store at *dest Return 0 if out of bounds, 1 if successful Similar to array implementations in Pascal, Java E.g., always do bounds checking
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Vector ADT vec_ptr new_vec(int len) { /* allocate header structure */ vec_ptr result = (vec_ptr) malloc(sizeof(vec_rec)) ; if ( !result ) return NULL ; result->len = len ;
Background image of page 8
9 Vector ADT /* allocate array */ if ( len > 0 ) { data_t *data = (data_t *)calloc(len, sizeof(data_t)) ; if ( !data ) { free( (void *)result ) ; return NULL ; /* couldn’t allocte stroage */ } result->data = data } else result->data = NULL return result ; }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Vector ADT /* * Retrieve vector element and store at dest. * Return 0 (out of bounds) or 1 (successful) */ int get_vec_element(vec_ptr v, int index, data_t *dest) { if ( index < 0 || index >= v->len) return 0 ; *dest = v->data[index] ; return 1; }
Background image of page 10
11 Vector ADT /* Return length of vector */ int vec_length(vec_ptr) { return v->len ; } /* Return pointer to start of vector data */ data_t *get_vec_start(vec_ptr v) { return v->data ; }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Optimization Example #ifdef ADD #define IDENT 0 #define OPER + #else #define IDENT 1 #define OPER * #endif
Background image of page 12
13 Optimization Example void combine1(vec_ptr v, data_t *dest) { int i; *dest = IDENT; for (i = 0; i < vec_length(v); i++) { data_t val; get_vec_element(v, i, &val); *dest = *dest OPER val; } }
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 Optimization Example Procedure Compute sum (product) of all elements of vector Store result at destination location
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/19/2011 for the course CS 000 taught by Professor Jing during the Fall '10 term at Fudan University.

Page1 / 48

26 - Code Optimization 1 Outline Codemotion Memoryoptimization Suggestedreading 5.3~5.6 2 Motivation easilysee10: algorit

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online