11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

82 Chap. 3 <strong>Algorithm</strong> <strong>Analysis</strong>deal with vari<strong>at</strong>ions of this problem. Most such implement<strong>at</strong>ions involvethe following tuning step. Directly testing whether a given complex objectcontains the point in question is rel<strong>at</strong>ively expensive. Instead, we canscreen for whether the point is contained within a bounding box for theobject. The bounding box is simply the smallest rectangle (usually definedto have sides perpendicular to the x <strong>and</strong> y axes) th<strong>at</strong> contains the object.If the point is not in the bounding box, then it cannot be in the object. Ifthe point is in the bounding box, only then would we conduct the full comparisonof the object versus the point. Note th<strong>at</strong> if the point is outside thebounding box, we saved time because the bounding box test is cheaper thanthe comparison of the full object versus the point. But if the point is insidethe bounding box, then th<strong>at</strong> test is redundant because we still have to comparethe point against the object. Typically the amount of work avoided bymaking this test is gre<strong>at</strong>er than the cost of making the test on every object.Example 3.20 Section 7.2.3 presents a sorting algorithm named SelectionSort. The chief distinguishing characteristic of this algorithm is th<strong>at</strong>it requires rel<strong>at</strong>ively few swaps of records stored in the array to be sorted.However, it sometimes performs an unnecessary swap oper<strong>at</strong>ion where ittries to swap a record with itself. This work could be avoided by testingwhether the two indices being swapped are the same. However, this eventdoes not occurr often. Because the cost of the test is high enough comparedto the work saved when the test is successful, adding the test typically willslow down the program r<strong>at</strong>her than speed it up.Be careful not to use tricks th<strong>at</strong> make the program unreadable. Most code tuningis simply cleaning up a carelessly written program, not taking a clear program<strong>and</strong> adding tricks. In particular, you should develop an appreci<strong>at</strong>ion for the capabilitiesof modern compilers to make extremely good optimiz<strong>at</strong>ions of expressions.“Optimiz<strong>at</strong>ion of expressions” here means a rearrangement of arithmetic or logicalexpressions to run more efficiently. Be careful not to damage the compiler’s abilityto do such optimiz<strong>at</strong>ions for you in an effort to optimize the expression yourself.Always check th<strong>at</strong> your “optimiz<strong>at</strong>ions” really do improve the program by runningthe program before <strong>and</strong> after the change on a suitable benchmark set of input. Manytimes I have been wrong about the positive effects of code tuning in my own programs.Most often I am wrong when I try to optimize an expression. It is hard todo better than the compiler.The gre<strong>at</strong>est time <strong>and</strong> space improvements come from a better d<strong>at</strong>a structure oralgorithm. The final thought for this section isFirst tune the algorithm, then tune the code.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!