67_pdfsam_cs2022_lectures - Optimizing code Turn on...

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

View Full Document Right Arrow Icon
Optimizing code Turn on automatic compiler optimizations Modern compilers have sophisticated algorithms that optimize code automatically Often 5-10x speed up e.g. -O3 flag to gcc Reduce memory use Remove unnecessary variables Reuse memory buffers Use the narrowest types possible (e.g. shorts instead of ints).
Background image of page 1

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

View Full DocumentRight Arrow Icon
Optimizing code Reduce number of function calls Change function into a macro (with #define) Use iterative instead of recursive algorithms Put redundant computations outside loops void compute(int A*, int N) { int j; for(j=0; j<N; j++) A[j] /= sqrt(2); } void compute(int A*, int N) { int j; double sqrt_2= sqrt(2); for(j=0; j<N; j++) A[j] /= sqrt_2; }
Background image of page 2
Integer arithmetic faster than floating arithmetic Use fastest operation possible bitwise operations (fastest) addition/ subtraction multiplication division (slowest) void compute(int A*, int N) { int j; double sqrt_2= sqrt(2); for(j=0; j<N; j++)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell University (Engineering School).

Page1 / 6

67_pdfsam_cs2022_lectures - Optimizing code Turn on...

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

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