Lect 34
Lect 34
Lecture 34:
Code Optimization
Felix Hernandez-Campos
April 17
Hidden Contants
3000
2500
Execution Time
2000
100*n+100
1500
n*n*n+10
1000
500
0
0 5 10 15
Problem Size
• Strength reduction
– Use the fastest version of an operation
– E.g.
x >> 2 instead of x / 4
x << 1 instead of x * 2
• Code motion
– Invariant expressions should be executed only once
– E.g.
for (int i = 0; i < x.length; i++)
x[i] *= Math.PI * Math.cos(y);
• Loop unrolling
– The overhead of the loop control code can be reduced by
executing more than one iteration in the body of the loop
– E.g.
double picosy = Math.PI * Math.cos(y);
for (int i = 0; i < x.length; i++)
x[i] *= picosy;
• Generating
highly
optimized is a
complicated
process
• We will
concentrate
on execution
speed
optimization
• Arcs represent
interblock control
flow
COMP 144 Programming Language Concepts
Felix Hernandez-Campos
15
Peephole Optimization
• Read Scott
– Ch. 13 intro
– Sect. 13.1
– Sect. 13.2
• Doug Bell, Make Java fast: Optimize!
– http://www.javaworld.com/javaworld/jw-04-1997/jw
-04-optimize_p.html