Separating Preprocessing from Query Cost

Some algorithms invest work once to make later queries cheaper. Compare the combined costs over the expected number of queries.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Use a static list of readings
  2. Ask how updates change the comparison
  3. Compare whole workload totals before choosing an index
  4. Include updates and one-time work honestly

Use a static list of readings

For \(n\) readings, prefix sums require \(O(n)\) work and allow constant-time range sums in a suitable arithmetic model. Scanning a large range for every query repeats work. Prefix range lookups give the exact arithmetic.

Ask how updates change the comparison

A changed early reading can invalidate many prefixes. Rebuilding before each query may remove the gain. Time and extra space guide whether retaining an index fits the workload.[1]

Compare whole workload totals before choosing an index

Suppose three thousand values are scanned for each sum query, or a prefix table is built with three thousand additions and then uses one subtraction per lookup. The benefit depends on how much of the array each direct query actually scans and how often the underlying values change. See prefix sum queries.

100 full-range scans: about 300,000 element additions
Build then 100 queries: about 3,000 additions plus 100 subtractions
Both ignore input and output overhead in this model

Include updates and one-time work honestly

A static benchmark with the index already built shows query latency but conceals setup. A changing data source may make the index expensive to maintain. Measure build time, update cost and query time separately before adding them for an expected workload.

References

  1. Jeff Erickson, Algorithms ↩