{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

67_pdfsam_cs2022_lectures

# 67_pdfsam_cs2022_lectures - Optimizing code Turn on...

This preview shows pages 1–6. Sign up to view the full content.

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

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

View Full Document
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; }
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;

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}