Software Design (CSC-223 97F)

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Studies] [Examples] [Readings] [Projects] [API]

# Outline of Class 28: Efficiency, Continued

## The Present-Value Problem, Again

• Although we saw in class that for simple problems (e.g., a single computation of the present value of an investment) any of our solutions are sufficiently fast, we'll continue to investigate optimizations.
• Why? Because in a typical application that uses present value, we'll need to do the computations thousands if not millions of times (e.g., each customer has multiple investments ...).
• Today, we'll look at two types of optimizations:
• Using tables (precomputed and not precomputed)
• Using integers instead of floating point numbers.

### Table-Based Solutions, Revisited

• In building a table-based solution, how can we best decide what values to use in building the table?
• Do we need many different initial investments? No. We can just use an initial investment of \$1.00 and multiply our final result by the actual initial investment.
• Do we need all years? No. We can use the table repeatedly. For example, if the table only stores values for 1 to 10 and we need to compute the result for thirteen years, we can multiply the values for three years and ten years.
• Do we need all the percentages? Perhaps. It is possible to interpolate between percentages, but it's nontrivial.
• What year values should we store in the table? If we store 1-10, we make small calculations easier, but it doesn't really help for large calculations. For example, to compute the value after 1000 years, we'll need to multiply the ten-year calculation by itself 100 times.
• Using this analysis, we might build tables for 1, 10, 100, 1000, etc. years.
• Or, better yet, build tables for 1, 2, 4, 8, 16, 32, ... years. Then, to compute the present value for a given number of years, you multiply the entries corresponding to each pit in the year. For example, 100 years is 64 + 32 + 4 + 2 years, so we multiply those entries.
• In fact, we can compute the table "on the fly" as it were.
```/**
Build a simple interest rate table.
The ith position in the table gives the value after 2^i years.
*/
public static double[] rateTable(int years, int rate) {
// The number of positions to fill in
int positions = log_2(years);
// Our new rate table.  Note we're trading a little extra
// space and one extra computation for a simpler comparison.
double[] rates = new double[positions+1];
rates[0] = rate;
// Fill it in
for (int i = 1; i < positions; ++i)
rates[i] = rates[i-1]*rates[i-1];
// That's it
return rates;
} // rateTable
```
• Moral: Radically different methods of looking at the same problem can help. They may also lead to new solutions to old problems (e.g., an efficient exponentiation algorithm) and techniques (e.g., dynamic programming).

Outlines: prev next 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

[News] [Basics] [Syllabus] [Outlines] [Assignments] [Studies] [Examples] [Readings] [Projects] [API]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.