E-means vs. k-means: How much faster can you build a vector index?
I tested Meta's e-means in Faiss on nearly a million embeddings to measure which construction shortcuts save time and what they cost at query time.
Takeaways
- Meta's e-means algorithm can cut IVF index construction time by up to 6.6× versus default Lloyd k-means (56.7s to 8.6s on the largest tested configuration) when using a pinned Faiss source build with two training epochs and reused cluster assignments.
- Shorter Lloyd training (3 iterations vs. 10) achieves similar build-time savings to two-epoch e-means but requires 19% more search work to hit 90% recall, while two-epoch e-means needs only ~4% more work than fully trained Lloyd.
- Reusing e-means' final-epoch assignments instead of running a separate assignment pass is safe at two or more epochs (≥95.5% label agreement, <0.12 pp recall penalty), but costs 1.5–3.1 pp of recall at just one epoch.
- An experimental Balanced e-means variant (5 epochs, 29.9s build) reduces mean search work by 8.9% and p99 vector scan counts by ~29% compared to default Lloyd, by enforcing more equal inverted-list sizes.
- The construction-time savings from faster builds are eventually offset by higher per-query scan costs; for the two-epoch label-reuse recipe, a hypothetical 10 µs query overhead would erase the ~48s build savings after roughly 4.8 million queries.
You're testing a new embedding model for your retrieval system. Before you can evaluate its performance with approximate search, you need to build a vector index. You could shorten the index's clustering stage to speed up the experiment, but how would that affect subsequent searches?
An IVF index groups similar embeddings so that each query examines only a fraction of the collection. Conventional k-means constructs these groups through repeated passes over training vectors. Poorly constructed groups can cause queries to miss nearest neighbors or examine more vectors to achieve the same retrieval recall.
Meta's e-means offers an alternative. It can train centroids in fewer passes and reuse cluster assignments instead of computing them again. Our earlier deep dive explains how the algorithm works.
To investigate these shortcuts, I tested two Faiss implementations on nearly a million arXiv embeddings.
On the largest configuration, e-means cut index construction time from 56.7 seconds to 8.6 on a pinned source build. Recall@10 increased from 91.39% to 91.81% at the same search setting, but the index scanned 4% more vectors at the evaluated 90% recall target. An experimental balanced variant took 29.9 seconds to build but scanned 8.9% fewer vectors than conventional k-means.
Why index construction affects search
Suppose you have a million document embeddings and want to retrieve the ten closest matches to a query. An exhaustive search compares the query with every vector in the collection. Its computational cost grows with the number and dimensionality of the vectors.
An inverted file (IVF) index partitions the collection into groups called inverted lists. Each list has a representative vector, or centroid. At query time, the index finds the nearest centroids and searches their associated lists. The nprobe parameter determines how many lists it examines.
If you distribute 996,721 vectors evenly across 4,096 lists, a query that probes 12 lists examines approximately 2,920 vectors, excluding the initial centroid comparisons.
In my largest index, queries on Faiss's default k-means build scanned approximately 3,234 vectors on average at nprobe=12, around 11% more than an even partition.
Some queries encounter larger lists than others. A query's nearest neighbors can also reside in lists whose centroids fall outside its search range. Both the placement of vectors and the size of each list affect retrieval performance.
Building a Faiss IVF-Flat index from existing embeddings involves four stages:
- Sample: Select training vectors. Faiss uses at most 256 vectors per centroid by default.
- Train: Learn one centroid per inverted list through clustering.
- Assign: Find the nearest centroid for every vector in the collection.
- Insert: Store each vector in its assigned list.
The relative cost of each stage depends on the index configuration. In my released-version experiments with 996,721 vectors and 256 lists, centroid training accounts for approximately a quarter of construction time. With 4,096 lists, it accounts for 47 of the build's 52 seconds.
If you build a small index once and serve it for months, saving a few seconds during construction may not matter. If you repeatedly build indexes to evaluate embedding models, change partition configurations or rebuild document collections, those costs accumulate.
What e-means changes
Faiss uses Lloyd's algorithm for conventional k-means clustering. Each iteration assigns training vectors to their nearest centroids and recalculates the centroids from their assigned vectors. The centroids remain fixed until the algorithm completes a pass through the training data.
Faiss's default IVF configuration performs ten iterations.
Meta's e-means divides the training data into mini-batches and updates centroids throughout each epoch through an exponential moving average. On the paper's benchmarks, this approach reaches a given clustering loss in fewer passes than Lloyd's algorithm.
Mini-batch updates introduce their own computational overhead, however. Faster centroid training also leaves the cost of vector assignment and insertion unchanged unless you modify those stages.
I wanted to measure the cost of constructing a searchable index, rather than centroid training alone, and evaluate the retrieval performance of the resulting index.
How I tested
| Parameter | Configuration |
|---|---|
| Dataset | 998,721 arXiv titles and abstracts from 2020–2024 |
| Embedding model | BAAI/bge-small-en-v1.5 |
| Embeddings | 384 dimensions, normalized, float32 |
| Index | Faiss IVF-Flat, L2 distance |
| Index sizes | 100,000 and 996,721 vectors |
| Number of lists | 256, 1,024 and 4,096 |
| Evaluation queries | 2,000 held-out papers |
| Clustering seeds | 3 |
| Hardware | Apple M4 CPU, four threads |
I tested five combinations of index size and list count. I excluded the 100,000-vector configuration with 4,096 lists because it falls below the experiment's minimum of 39 training vectors per centroid.
The experiments compare Lloyd at three, ten and 25 iterations against e-means at two, three and five epochs. Both algorithms receive identical training samples within each paired drop-in comparison. Separate experiments evaluate assignment reuse and Balanced e-means.
I tested two Faiss implementations:
- Released:
faiss-cpu==1.16.0from PyPI. - Source: Commit
ab0e8b9f22from Faiss PR #6000, which includes compiled CPU accumulation and experimental Balanced e-means support.
Construction time includes sampling, training, vector assignment and insertion. It excludes embedding generation, index serialization and evaluation.
I measure retrieval accuracy through Recall@10: the proportion of each query's ten exact nearest neighbors that the index returns.
For each workload and software build, I select the smallest tested nprobe at which default Lloyd reaches 90% recall on its first seed. All methods use this setting for fixed-nprobe recall comparisons. I use the same evaluation queries to select the setting, so it does not represent independently tuned deployment performance.
Why can five e-means epochs take longer than ten Lloyd iterations?
e-means uses five epochs by default, half the number of iterations in Faiss's standard IVF training. Despite this, default e-means constructs indexes more slowly than default Lloyd in four of the five configurations on the released implementation.
The pinned source build produces different results.
| Vectors | Lists | Released e-means speedup | Source e-means speedup |
|---|---|---|---|
| 100k | 256 | 0.31× | 0.75× |
| 100k | 1,024 | 0.70× | 1.37× |
| 997k | 256 | 0.63× | 1.05× |
| 997k | 1,024 | 0.76× | 1.29× |
| 997k | 4,096 | 1.26× | 2.09× |
Median paired construction-time ratios across three seeds, relative to ten-iteration Lloyd within each software build.
I reached out to Swann Bessa, an author of e-means, to understand why the algorithm takes longer on smaller workloads.
"Because e-means uses mini-batches, one epoch can be a bit slower than a Lloyd iteration on very small sets, roughly below n × k ≈ 10⁹," Bessa told AlphaSignal.
Here, n represents the number of training vectors and k represents the number of centroids.
In the smallest released-version configuration, an e-means epoch takes approximately 7.8 times as long as a Lloyd iteration on average. In the largest, this ratio falls to 1.5. With five epochs versus ten iterations, e-means needs its average epoch time to fall below twice Lloyd's average iteration time for total training to be faster.
Faiss 1.16.0 performs part of its centroid accumulation through NumPy. The overhead can account for a larger proportion of execution time on smaller workloads. The source implementation moves this accumulation into compiled code, alongside other implementation changes.
On the pinned source build, default e-means constructs indexes faster than default Lloyd in four configurations, with a speedup of up to 2.09×. The experiment does not isolate how much of this improvement comes from the compiled accumulation alone.
How few training passes can you get away with?
One way to reduce construction time is to lower the number of Lloyd iterations.
On the largest released-version configuration, reducing Lloyd from ten iterations to three cuts construction time from 52.4 seconds to 19.8 seconds. Two epochs of e-means construct an index within almost the same time budget.
| Metric | Lloyd, 3 iterations | e-means, 2 epochs |
|---|---|---|
| Construction time | 19.8s | 19.6s |
| Recall@10 at nprobe 12 | 90.15% | 91.74% |
| Extra vectors scanned at 90% recall | +19.3% | +2.8% |
Search work uses the first evaluated nprobe that reaches 90% recall for each seed, relative to default Lloyd.
The three-iteration Lloyd index needs considerably more search work to reach the recall target, despite taking approximately the same time to construct.
On the source build, two-epoch e-means also becomes the faster option: 13.3 seconds compared with 20.4 seconds for three-iteration Lloyd. Across all five source-build workloads, two-epoch e-means achieves median paired construction speedups of 1.16–3.77× over default Lloyd.
Additional training offers limited retrieval gains in these experiments. On the released implementation, 25 Lloyd iterations take 1.3–2.3 times as long as ten iterations but improve observed Recall@10 by only 0.04–0.54 percentage points. Three of the five confidence intervals include zero.
"Lower clustering loss does not necessarily mean better retrieval," Bessa explained. "In practice, I would track recall against epochs, at a fixed nprobe or a fixed number of scanned vectors, and stop when it flattens."
In all five released-version configurations, 25 Lloyd iterations produce lower nearest-centroid distortion over the indexed documents than two e-means epochs. Yet two-epoch e-means achieves higher observed recall in three of them, with differences of approximately 0.2 percentage points or less.
K-means minimizes the squared distance between vectors and their assigned centroids. IVF search needs a query's nearest neighbors to reside within the lists it examines. Lower distortion does not guarantee that a query finds more of those neighbors.
Measure recall and search work across training budgets before you decide how many passes your index needs.
Can you skip the assignment pass?
After conventional centroid training, IVF construction requires another nearest-centroid assignment pass to determine where each vector belongs.
e-means already computes these assignments during training. Its implementation can retain the labels from the final epoch and use them directly to populate inverted lists.
Bessa also suggested testing this approach:
"As long as you train for enough epochs, you can use the labels e-means returns instead of a separate assignment pass. Three epochs should be enough in practice."
The complication is that e-means updates its centroids throughout an epoch. A vector that the algorithm processes near the beginning of that epoch may have a different nearest centroid by the time training finishes.
To isolate the effect of assignment reuse, I construct two indexes from the same trained centroids. One recomputes the nearest-centroid assignments. The other uses the labels returned by e-means.
| Training budget | Label agreement, released | Recall change, released | Recall change, source |
|---|---|---|---|
| 1 epoch | 76–86% | −3.14 to −1.54 pp | −1.87 to −1.27 pp |
| 2 epochs | 95.5–96.7% | −0.12 to −0.07 pp | −0.11 to −0.03 pp |
| 3 epochs | 97.5–98.2% | −0.04 to +0.003 pp | −0.05 to −0.01 pp |
Ranges of mean results across five workloads per implementation. Recall changes compare reused assignments with fresh assignments from identical centroids.
After one epoch, as many as 24% of the returned labels disagree with a fresh nearest-centroid assignment on the released implementation. Reusing them costs between 1.5 and 3.1 percentage points of recall.
After two epochs, approximately 96% of the labels agree with fresh assignments, and the recall difference falls to around a tenth of a percentage point.
All 20 two- and three-epoch comparisons across both implementations meet the experiment's 0.5-percentage-point recall tolerance relative to fresh assignments. The lower bounds of the paired 95% query-bootstrap confidence intervals remain above −0.5 points, conditional on the three trained seeds.
An assignment mismatch does not necessarily cause a retrieval error. A query that probes two nearby lists can retrieve a vector regardless of which of those lists contains it. This could explain the small recall penalty despite imperfect assignment agreement, although I did not measure where the mismatched vectors move.
| Two-epoch e-means | Released build time | Released recall | Source build time | Source recall |
|---|---|---|---|---|
| Fresh assignments | 19.10s | 91.62% | 13.98s | 91.84% |
| Reused assignments | 14.90s | 91.55% | 8.61s | 91.81% |
996,721 vectors, 4,096 lists, nprobe 12. Median construction times and mean Recall@10 across three seeds. Both assignment variants use identical trained centroids within each implementation.
On the source build, reusing labels saves 5.37 seconds based on the median construction times. The recall difference is −0.033 percentage points, with a 95% query-bootstrap confidence interval of −0.093 to +0.030 points.
Compared with default Lloyd's 56.7-second build, the complete two-epoch label-reuse recipe reduces construction time by approximately 85%, equivalent to a 6.6× speedup from the median times.
Label reuse is already a documented e-means feature. These experiments measure the retrieval consequences of using it at different training budgets. On this corpus, two epochs suffice to meet the specified recall tolerance relative to fresh assignments.
Build an IVF index directly from e-means assignments
Faiss's add_preassigned function inserts vectors into inverted lists without recomputing their nearest centroids.
The following example uses the faiss-cpu==1.16.0 API.
import faiss
import numpy as np
from faiss.contrib.e_means import NumpyEmeans
from faiss.contrib.ivf_tools import add_preassigned
# x contains every vector to index
x = np.ascontiguousarray(x, dtype=np.float32)
d = x.shape[1]
nlist = 4096
# Train centroids and retain assignments
em = NumpyEmeans(d, nlist, nepochs=2)
centroids, labels = em.train(x)
# Initialize the IVF quantizer
quantizer = faiss.IndexFlatL2(d)
quantizer.add(centroids)
index = faiss.IndexIVFFlat(quantizer, d, nlist)
# Insert vectors using existing assignments
add_preassigned(index, x, labels)
index.nprobe = 12
The returned labels must cover every vector you intend to insert. Each captured label requires eight bytes of storage, or approximately 8 MB for one million vectors.
Verify recall against an index that uses fresh assignments before you adopt this approach. The appropriate training budget and nprobe depend on your data.
What does a faster build cost at query time?
Conventional k-means minimizes clustering distortion without explicitly enforcing equal list sizes. Queries that probe the same number of lists can therefore examine different numbers of vectors.
"Balance matters much more. Balancing the clusters gives both better recall and less variance in query latency," Bessa told AlphaSignal.
To examine the effect of balance on search work, I tested Balanced e-means, an experimental extension available in Faiss PR #6000.
Balanced e-means introduces a learnable bias for each cluster. During training, the algorithm uses these biases to adjust the squared distances that determine cluster assignments and encourage more even partitions.
I tested a BIVF construction recipe with five and 15 training epochs. I disabled the separate bias-polishing phase and used fresh nearest-centroid assignments to populate the index. This differs from the PR's default Balanced e-means configuration.
On the largest workload, the five-epoch recipe reduces the list imbalance factor from 1.169 to 1.014, where 1 represents perfectly equal list sizes.
| Build recipe | Build time | Recall@10 | Extra mean scan work at 90% recall | p99 vectors scanned |
|---|---|---|---|---|
| Default Lloyd | 56.7s | 91.39% | Baseline | 4,487 |
| Lloyd, 3 iterations | 20.4s | 90.15% | +19.3% | 4,464 |
| e-means, 5 epochs | 25.0s | 92.01% | +4.9% | 5,035 |
| e-means, 2 epochs, drop-in | 13.3s | 91.78% | +4.2% | 5,240 |
| e-means, 2 epochs, reused labels | 8.6s | 91.81% | +4.0% | 5,146 |
| Balanced e-means, 5 epochs | 29.9s | 91.20% | −8.9% | 3,166 |
| Balanced e-means, 15 epochs | 77.3s | 91.49% | −8.9% | 3,156 |
Pinned PR build, 996,721 vectors, 4,096 lists. Recall and p99 scan counts use nprobe 12. Construction times are medians, recall values are means, and p99 scan counts are means of per-seed 99th percentiles. Additional mean scan work uses the first evaluated nprobe that reaches 90% recall for each seed. The two-epoch drop-in and label-reuse configurations use separate training runs.
Two-epoch e-means with reused labels constructs the index in 8.6 seconds but requires 4% more mean search work than default Lloyd at the evaluated recall target.
Five-epoch Balanced e-means takes 29.9 seconds to construct but scans 8.9% fewer vectors at that target. At nprobe=12, it also reduces p99 scan counts by 29%, although recall is slightly lower than Lloyd's.
Across all five workloads, the five-epoch balanced recipe reduces fixed-nprobe p99 scan counts by approximately 29–33% relative to default Lloyd. On the largest workload, increasing its training budget to 15 epochs adds approximately 47 seconds to construction with almost no further reduction in scan counts.
These results measure the number of vectors examined. They do not establish proportional changes in latency, throughput or infrastructure expenditure.
When do the build savings run out?
For an index that serves Q queries between builds, a simple additive runtime model gives:
Total runtime = build time + Q × mean query time
The two-epoch label-reuse recipe saves approximately 48 seconds of construction time relative to default Lloyd on the source build.
If it adds 10 microseconds to the average query time, those construction savings disappear after approximately 4.8 million queries.
Break-even queries = 48 / 0.00001 = 4.8 million
The additional 10 microseconds is hypothetical. The actual break-even point requires comparable query timings at search settings that meet the same retrieval requirements.
A system that constructs indexes frequently can therefore have different runtime requirements from one that serves millions of queries between rebuilds.
How to test these shortcuts on your own embeddings
| If you encounter this problem | What to test |
|---|---|
| Centroid training dominates construction time | Compare Lloyd and e-means across training budgets and software versions. |
| You already shorten Lloyd training | Compare low-epoch e-means at a similar construction cost. |
| Vector assignment takes a substantial share of construction time | Test returned labels against fresh assignments, provided the labels cover every indexed vector. |
| Your index serves a large query volume | Compare mean search work and measured query time at the required recall. |
| Tail query performance matters | Evaluate list imbalance, p99 scan counts and actual latency. |
Sweep nprobe independently for each index and test integer settings near your recall target.
On the largest released-version configuration, interpolation between tested nprobe values suggests that two-epoch e-means scans 1.9% fewer vectors than default Lloyd to reach 90% recall.
At the first tested setting that reaches 90%, it scans 2.8% more.
Around this point, the experiment tests nprobe values of 8, 12 and 16, but not every integer between them. An interpolated recall–work curve can help identify promising settings, but measure those settings before you deploy them.
To run these comparisons on your own embeddings, use the audit script that accompanies this article. It builds the default k-means index alongside three-iteration k-means, two-epoch e-means, two-epoch e-means with reused labels (only when the default training sample already covers every vector) and, on Faiss builds that include it, Balanced e-means. For each index, it sweeps every integer nprobe and reports build time, Recall@10, mean and p99 vectors scanned and measured search time at the first setting that reaches your recall target. It then estimates the query volume at which each faster build's savings run out.
python ivf_build_audit.py embeddings.npy --nlist 4096 --normalize --target-recall 0.9 --seeds 3 --queries-per-build 1000000
What these results don't tell you
When to rebuild an index. I measure fresh index construction. These results do not establish how quickly centroids become stale as documents arrive or whether cheaper clustering makes more frequent rebuilding worthwhile. My next experiment examines quarterly arXiv updates across 2022–2024.
Performance across other hardware and datasets. The experiments use one CPU platform, one corpus, one embedding model and IVF-Flat. They do not cover GPUs or compressed indexes such as IVF-PQ, which introduce additional training stages.
Production query performance. The evaluation uses held-out academic papers as queries. Scan counts measure search work but do not establish serving latency under concurrent production traffic.
End-to-end application quality. Recall@10 measures agreement with exact nearest-neighbor search. It does not establish whether retrieved documents are relevant to a user's question or whether a RAG system produces better answers.