Presentation is loading. Please wait.

Presentation is loading. Please wait.

Optimization.

Similar presentations


Presentation on theme: "Optimization."— Presentation transcript:

1 Optimization

2 How to Optimize Code Conventional Wisdom: Don't do it
(For experts only) Don't do it yet

3 Why Not??? You can't optimize something that doesn't work
Biggest improvement is picking the right algorithm 90 / 10 rule 90% of the time is spent in 10% of the code Need to be able to verify speedup Optimized code is usually: Less readable/maintainable More complex/brittle

4 Code Tricks Prefer ints to floats Minimize float->int conversions
Use multiplication instead of division

5 Strength Reductions Avoid costlier operations (divide/multiply) by using cheaper ones (add/subtract/shift) int total = 0; for (i = 0; i < 1000; i++) total += i * 3; int total = 0; for (i = 0; i < 3000; i+=3) total += i;

6 Fancy Tricks Make if the common case:
If path usually gets unconditional branch Static branch prediction usually skips forward branches Else relies on conditional branch

7 Fancy Tricks Loop unrolling :
Reducing iterations by increasing work done per step Less Overhead Gives compiler/processor more to rearrange for (i = 1; i <= 30; i++) a[i] = a[i] + b[i] * c; for (i = 1; i <= 30; i+=3) { a[i] = a[i] + b[i] * c; a[i+1] = a[i+1] + b[i+1] * c; a[i+2] = a[i+2] + b[i+2] * c; }

8 Fancy Tricks Loop fusion:
Combine loops doing unrelated jobs to avoid overhead Will they fight over cache??? for (i = 0; i < N; i++) C[i] = A[i] + B[i]; D[i] = E[i] + C[i]; for (i = 0; i < N; i++) C[i] = A[i] + B[i]; D[i] = E[i] + C[i]; }

9 Fancy Tricks Loop peeling:
Break up loops to avoid branches within them for (i = 1; i <= N; i++) { if (i==1) A[i] = 0; else if (i == N) A[i] = N; else A[i] = A[i] + 8; } A[1] = 0; for (i = 2; i < N; i++) A[i] = A[i] + 8; A[N] = N;

10 How To Optimize - Branching
Pipelined processors HATE branch miss- predictions 15+ cycle penalty

11 How To Optimize - Branching
May be able to rearrange data for consistent branching Some compilers can use runtime profiles to reorder code Come compilers allow "expect" – hints about how to order instructions

12 How To Optimize - Branching
Maybe better off without it:

13 How To Optimize - Branching
Maybe better off without it:

14 How To Optimize - Cache Cache: Modern CPU's live/die by cache
Like contiguous data Avoid repeated swapping between multiple items 25:00-35:00 and beyond

15 How to Optimize Right Rules
Start with clean, working code and good tests Test any optimizations If it doesn't help, take it out

16 Compiler Optimization
Compilers can Eliminate unneeded code Do strength reductions Unroll loops Compiler likes specific information Use constants Make variables as local as possible

17 Samples O0 : Faithful (but efficient) line by line translation O1 :
Try to do work in registers instead of stack frame Remove obviously worthless code O2 : Aggressively do work ahead of time

18 Samples C++ Code:


Download ppt "Optimization."

Similar presentations


Ads by Google