Was Our Objective Met?


This applet above allows you to select from the various line drawing algorithms discussed. You can draw lines using the selected algorithm by clicking and dragging with the first mouse button. You can also time the algorithms by clicking on Benchmark. In order to get more accurate timings the pattern is drawn five times (without clearing), and the final result is displayed.

To the left is a benchmarking applet



Notes:
  • modern compilers will often find these sorts of optimizations

  • Dilemma:
    Is it better to retain readable code, and depend a compiler to do the optimization implicitly, or code the optimization explicitly with some loss in readability?

Lecture 5 Slide 11 6.837 Fall '98

Next, we need to verify that our objective was met. For that purpose we can use the benchmark applet.

A parting note: Many modern optimizing compilers will automatically find the sorts of optimizations that we applied here (inlining function calls within loops and converting functions to incremental calculation). In order to check if your compiler is so clever, you'll need to do one of two things: look at the code it generated or write a test fragment to time your optimized code.

This raises the question: Is it better to retain readable code, and depend a compiler to do the optimization implicitly, or code the optimization explicitly with some loss in readability? The answer is not clear cut. In general, you should make your code as readable as possible. Comments can go a long way in addressing this in optimized code. On the other hand, you should also seldom trade-off portability for elegance. Thus, if your application depends on a fast algorithm, you are better off coding directly rather than requiring on a compiler to do your work, because in all likelyhood, compiler versions and platforms will change more frequently than your code!