1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 34: Code Optimization COMP 144 Programming Language Concepts Spring 2002 Felix.

Download 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 34: Code Optimization COMP 144 Programming Language Concepts Spring 2002 Felix.

Post on 21-Dec-2015

212 views

Category:

Documents

0 download

TRANSCRIPT

  • Slide 1
  • 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 34: Code Optimization COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos April 17 The University of North Carolina at Chapel Hill
  • Slide 2
  • 2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Optimization We will discuss code optimization from the point of view of the compiler and the programmerWe will discuss code optimization from the point of view of the compiler and the programmer Code improvement is an important phase in production compilersCode improvement is an important phase in production compilers Generate code that runs fast (CPU-oriented) In some case, optimize for memory requirements or network performance Optimization is an important task when developing resource-intensive applicationOptimization is an important task when developing resource-intensive application E.g. Games, Large-scale services
  • Slide 3
  • 3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Optimization Two lectures on optimizationTwo lectures on optimization ReferencesReferences Scotts Chapter 13 Doug Bell, Make Java fast: Optimize! http://www.javaworld.com/javaworld/jw-04-1997/jw-04- optimize_p.html http://www.javaworld.com/javaworld/jw-04-1997/jw-04- optimize_p.htmlhttp://www.javaworld.com/javaworld/jw-04-1997/jw-04- optimize_p.html HyperProf- Java profile browser http://www.physics.orst.edu/~bulatov/HyperProf/ http://www.physics.orst.edu/~bulatov/HyperProf/ Python Performance Tips http://manatee.mojam.com/~skip/python/fastpython.html http://manatee.mojam.com/~skip/python/fastpython.html Python Patterns - An Optimization Anecdote http://www.python.org/doc/essays/list2str.html http://www.python.org/doc/essays/list2str.html
  • Slide 4
  • 4 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Golden Rules of Optimization Premature Optimization is Evil Donald Knuth, premature optimization is the root of all evilDonald Knuth, premature optimization is the root of all evil Optimization can introduce new, subtle bugs Optimization usually makes code harder to understand and maintain Get your code right first, then, if really needed, optimize itGet your code right first, then, if really needed, optimize it Document optimizations carefully Keep the non-optimized version handy, or even as a comment in your code
  • Slide 5
  • 5 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Golden Rules of Optimization The 80/20 Rule In general, 80% percent of a programs execution time is spent executing 20% of the codeIn general, 80% percent of a programs execution time is spent executing 20% of the code 90%/10% for performance-hungry programs90%/10% for performance-hungry programs Spend your time optimizing the important 10/20% of your programSpend your time optimizing the important 10/20% of your program Optimize the common case even at the cost of making the uncommon case slowerOptimize the common case even at the cost of making the uncommon case slower
  • Slide 6
  • 6 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Golden Rules of Optimization Good Algorithms Rule The best and most important way of optimizing a program is using good algorithmsThe best and most important way of optimizing a program is using good algorithms E.g. O(n*log) rather than O(n 2 ) However, we still need lower level optimization to get more of our programsHowever, we still need lower level optimization to get more of our programs In addition, asymthotic complexity is not always an appropriate metric of efficiencyIn addition, asymthotic complexity is not always an appropriate metric of efficiency Hidden constant may be misleading E.g. a linear time algorithm than runs in 100*n+100 time is slower than a cubic time algorithm than runs in n 3 +10 time if the problem size is small
  • Slide 7
  • 7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Asymptotic Complexity Hidden Constants
  • Slide 8
  • 8 COMP 144 Programming Language Concepts Felix Hernandez-Campos General Optimization Techniques Strength reductionStrength reduction Use the fastest version of an operation E.g. x >> 2 instead of x / 4 x

Recommended

View more >