If you have a million data points, would you use a DNN or KNN? What is the difference in inference time and memory at prediction?

The Problem: A classifier built on 1,000,000 points of 256 float32 features must serve 2,000 predictions per second at a 10 ms p99 from a 4 GB CPU replica. Option A is KNN with k = 10 over every stored point, option B is a 256-512-256-10 MLP, and nobody has yet priced either one at prediction time.
Answer
At a million points with any real query volume, use a DNN (or a learned embedding served through an approximate nearest-neighbor index), not brute-force KNN. KNN is a lazy learner. Training is free because it only stores the data, but every prediction scans all stored points, so a query costs
time and the model occupies
memory. A DNN pays up front in training and then predicts in
time and memory, where the parameter count
does not grow with the dataset. With 256 float32 features, brute-force KNN holds 1.02 GB and spends
multiply-adds per query. A 265k-parameter MLP holds 1.06 MB and spends about
, a gap of roughly 1,000x on both axes. KNN stays competitive only behind an ANN index such as HNSW or IVF-PQ, or when the data changes faster than you can retrain.
(1) Lazy Versus Eager Learning: KNN defers all computation to prediction time, while a DNN compresses the training set into weights once. The choice is really about where the cost lands: training or every single query.
(2) Inference Time: Brute-force KNN computes distances of
multiply-adds each, plus an
top-k selection. A DNN forward pass costs
regardless of
, so at one million points the MLP finishes in tens of microseconds while KNN is memory-bandwidth bound at about 20 ms.
(3) Memory At Prediction: KNN must keep every training vector resident, bytes, and the footprint grows linearly as data arrives. The DNN keeps only its weights, so ten times more training data leaves its serving footprint unchanged.
(4) ANN Indexes Change The Math: HNSW visits roughly 1,500 nodes and reaches recall@10 near 0.95 in under a millisecond, but its graph links push memory to about 1.28 GB. IVF-PQ compresses each vector to 32 bytes (about 44 MB in total) at the price of approximate distances.
(5) Distance Quality: In 256 raw dimensions, distances concentrate and KD-trees degrade to brute force beyond roughly 20 dimensions. Raw Euclidean distance also weights every feature equally, while a DNN learns which features matter.
(6) When Lazy Learning Wins: Tiny query volume, data that must count the moment it arrives, a need for instance-level explanations, or cases where the neighbors themselves are the product, such as similar-item retrieval.
Mathematical Formulation:
Where:
is the prediction for query
,
is the stored label of training point
, and
holds the indices of the
stored points closest to
; regression replaces the mode with a mean.
is the number of multiply-adds per query for brute-force KNN over
stored points of dimension
; top-k selection adds
comparisons on top.
is the resident memory at prediction, with
bytes per number (
for float32).
sums
over
dense layers of widths
, with
, and
stores
parameters. Neither depends on
.
is DNN training compute over
epochs; the factor 3 counts one forward pass plus a backward pass that costs about twice the forward.
is the break-even number of predictions after which the DNN has spent less total compute than KNN, training included.
The formulas make the asymmetry explicit. appears in both of KNN’s inference terms, but for the DNN it appears only inside
, a one-time cost. So the question “DNN or KNN?” is really “how many predictions will this model serve before the data changes?” Any service answering more than
queries per model version favors the DNN, and
is usually tiny compared with production traffic.
Worked Example:
Use the scenario’s numbers: ,
, float32 so
, and an MLP with layers 256-512-256-10. Brute-force KNN holds
bytes and spends
MACs per query. The MLP spends
MACs and stores 265,482 parameters, or 1.06 MB. Training it for
epochs costs
MACs, and each prediction then saves about
MACs, so
. At 2,000 queries per second the DNN repays its whole training run in 31 seconds of traffic. Latency tells the same story: one unbatched KNN query streams 1.02 GB, about 20 ms at 50 GB/s, and 2,000 such queries per second would need about 2 TB/s of memory bandwidth, forty times what the replica has.

Figure 1: (a) Per-query multiply-adds against dataset size: brute-force KNN grows linearly, HNSW grows roughly with (schematic model of about
visited nodes), and the DNN is flat. (b) Memory held at prediction: at
brute force needs 1.02 GB, HNSW 1.28 GB, IVF-PQ about 44 MB, and the DNN 1.06 MB regardless of
. (c) Total compute including training: the DNN’s training bill of
MACs is repaid after about 62,000 predictions.
Panel (a) hides one subtlety that the table makes explicit. HNSW spends only slightly more multiply-adds per query than the MLP, yet it is still around ten times slower in wall-clock time, because graph traversal makes random memory accesses across a 1.28 GB structure while the MLP’s 1 MB of weights stays in CPU cache.
| Property (N = 1M, d = 256) | DNN (MLP 256-512-256-10) | Brute-force KNN | KNN + HNSW index |
|---|---|---|---|
| Training cost | About 1.6 x 10^13 MACs for 20 epochs; minutes on a GPU | Store the data; effectively zero | Graph build; a few minutes on a multi-core CPU |
| Memory at prediction | 1.06 MB of weights, independent of N | 1.02 GB, linear in N | About 1.28 GB (vectors plus 64 links per node) |
| Compute per query | 264,704 MACs | 2.56 x 10^8 MACs plus top-k selection | About 1,500 distance evaluations, roughly 4 x 10^5 MACs |
| Single-query latency (CPU) | Tens of microseconds, weights stay in cache | About 20 ms, bound by memory bandwidth | About 0.2 to 1 ms at recall@10 near 0.95 |
| Adding a new data point | Waits for the next retrain or fine-tune | Append one row; usable immediately | Online insert; deletes become tombstones |
| Fails when | Data drifts faster than the retraining cadence | Query volume, latency budget, or RAM is tight | Exact recall or heavy metadata filtering is required |
Leave a Reply