{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

26 - Code Optimization 1 Outline Codemotion...

Info icon This preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Code Optimization
Image of page 1

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

View Full Document Right Arrow Icon
2 Outline Machine-Independent Optimization Code motion Memory optimization Suggested reading 5.3 ~ 5.6
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
Image of page 3

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right 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 ;
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 ; }
Image of page 9

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

View Full Document Right 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; }
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 ; }
Image of page 11

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

View Full Document Right Arrow Icon
12 Optimization Example #ifdef ADD #define IDENT 0 #define OPER + #else #define IDENT 1 #define OPER * #endif
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; } }
Image of page 13

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

View Full Document Right Arrow Icon
14 Optimization Example Procedure Compute sum (product) of all elements of vector Store result at destination location
Image of page 14
15 Time Scales Absolute Time Typically use nanoseconds 10 –9  seconds Time scale of computer instructions
Image of page 15

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

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern