{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

67_pdfsam_cs2022_lectures

67_pdfsam_cs2022_lectures - Optimizing code Turn on...

Info iconThis preview shows pages 1–6. 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 Document Right 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
Optimizing code 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++) A[j] /= sqrt_2; } void compute(int A*, int N) { int j; double sqrt_half=sqrt(0.5); for(j=0; j<N; j++) A[j] *= sqrt_half;
Background image of page 3

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

View Full Document Right Arrow Icon
Background image of page 4
Background image of page 5

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

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

{[ snackBarMessage ]}