180 likes | 288 Vues
This guide provides a comprehensive overview of optimizing raster scanning programs, focusing on algorithmic, organizational, and low-level strategies. It discusses sorting algorithms, emphasizes the importance of inner loop optimization, and highlights timing techniques and clarity in code. Special cases are explored to enhance performance, notably by exploiting symmetry to reduce checks and accesses. Effective organization of main functions and structured handling of binary data will enable developers to create efficient, readable, and high-performance raster applications.
E N D
CS242 Optimizing the Raster Program
Optimization Priority • Algorithmic • Organizational/architectural • Low-level
Algorithmic Optimization • Can your improve on the method use to solve the problem? Example: sorting • O(n nog n) – Heap, Merge, quicksort • O(n^2) – Bubble, Insertion, Selection, Shell
Organizational/architectural(Is this algorithmic?) • Concentrate on optimizing the inner loops • Why? • How? • Reduce unnecessary operations
Low-level • Requires understanding compiler output • Knowing which combination of instructions will execute faster than others • Outside scope of this course
Check Timing • Where to put the checks • Why put them there? • See: http://rabbit.eng.miami.edu/info/functions/time.html
Timing the Raster Scanner • Place your START of timing right before you start actually scanning the raster and after your input phase • Place your END right after you have completed scanning but before you do your output • Why?
Some Words About Style • Your main() • Initialization (read in input) • Important work (scan the raster) • Output (output the results) • Cleanup (whatever housekeeping you need) • Write for clarity first • Avoid code that takes some thinking to understand
Clarity Objects in mirror appear farther than they actually are
Clarity Objects in mirror are closer than they appear
Objects in Mirror… • Why is the first choice preferred? • Closer • Farther
Some Optimizations • Create special cases to majority of cases run without checks • 9 special cases • Exploit the symmetry of equality • ½ the number of checks • Further reduces special cases • No longer check if left column
Special Cases9 Zones • Rows • Top row • Middle rows • Bottom row • Columns • Leftmost • Middle • Rightmost
Check 8 Neighbors Check boundary conditions
Symmetry of Equality • Reduces number of checks and element accesses by ½ • Reduces number of special case zones to 6
Assignment • Organize your main • Write for clarity • Optimize • Time your main work before optimization (last week’s code) • Time your new work • Be prepared for a data file that I give you
Handling binary data in C • Allocate memory for data • Load data into memory • Define “struct” matching header • Use pointers to point at the correct location • Cast pointers when necessary