Field notes
When K-Means Was Faster Than E-Means
I explored Meta's E-Means clustering algorithm, compared it with traditional K-Means, and discovered how dataset size, dimensionality, and batch configuration affect clustering performance.
Why I explored E-Means
I recently came across E-Means, a clustering algorithm introduced by researchers at Meta FAIR. The research caught my attention because it applies modern optimization techniques to the classical K-Means problem.
K-Means is one of the first unsupervised learning algorithms many of us learn. It is simple, widely used, and well understood. I wanted to know what E-Means changes, whether it is faster, whether it finds better clusters, and which workloads suit it.
Instead of relying only on the research, I compared both algorithms in Google Colab. Traditional K-Means was faster in my first experiment. The results changed when I increased the workload's complexity.
Understanding K-Means
K-Means groups data points into K clusters. For example, with 1,000 customers described by age and monthly spending, we could choose K = 5 and group customers around five centers.
The basic loop is:
- Choose K and initialize K centroids.
- Assign each point to its nearest centroid.
- Recalculate each centroid from its assigned points.
- Repeat until convergence or the iteration limit.
Classical Lloyd's K-Means scans the full dataset on each iteration. This works well, but repeated distance calculations can become expensive with many points, dimensions, or clusters. It can also converge to a poor local minimum.
What is E-Means?
E-Means is a different optimization method for the same K-Means objective. It uses mini-batches and exponential moving average (EMA) updates, among other changes. It does not define a different clustering problem.
With full-batch K-Means, each update considers all one million points. A mini-batch method processes smaller groups and updates cluster statistics over time. E-Means still makes multiple passes during training; mini-batches do not mean that it sees the data only once.
EMA combines earlier information with new observations while assigning more weight to recent information. E-Means uses EMA-based updates to maintain cluster counts and sums, then derives centroids from those statistics. Its implementation automatically configures batch and step settings. The research describes improvements to initialization and finalization, as well as reviving poorly used clusters by splitting a larger cluster.
The official implementation exposes Quality and Throughput modes. Their batch configurations can trade training time against clustering quality. Initialization and final refinement also affect the final result. These details matter when comparing runtime and loss.
How I ran the experiments
I built a small Google Colab notebook using Python, NumPy, Scikit-learn, Faiss, the official E-Means implementation, Matplotlib, and Pandas. I generated synthetic datasets with Scikit-learn's make_blobs; both algorithms received the same dataset in each comparison.
Open the Google Colab notebook used for these experiments.
For K-Means, I used Scikit-learn's KMeans with Lloyd's algorithm, random initialization, one initialization, at most 25 iterations, and zero convergence tolerance. E-Means used the official NumpyEmeans implementation for five epochs, with automatic batch configuration unless noted.
I measured training time and mean squared distance from each point to its nearest centroid (MSE). Lower MSE means lower loss under the K-Means objective. In the initial experiment, I also measured Adjusted Rand Index (ARI), which compares predicted clusters with known labels; 1 means perfect agreement. ARI was available because make_blobs supplied the original group labels. Training time excludes the separate MSE evaluation.
Experiment 1: Simple 2D clustering
I started with 60,000 points, two features, and twelve clusters. K-Means was about 5.8 times faster. E-Means had about 4.76% lower MSE and a higher ARI.
My first takeaway was that K-Means trained faster while E-Means found a lower-loss clustering. I wondered whether the small, simple dataset explained E-Means' runtime, so I increased the sample count.
Experiment 2: Increasing dataset size
I tested 100,000, 500,000, and 1,000,000 points, keeping two features and twelve clusters. In one initial run, E-Means appeared to get faster as the dataset grew. That looked suspicious: more data usually needs more computation, though timing can vary for many reasons.
I did not treat that single result as a conclusion. Warm-up, CPU scheduling, caching, initialization, and library behavior can all affect wall-clock time. I repeated the experiment.
Experiment 3: Repeated 2D benchmark
I warmed up both implementations, ran each configuration three times with different random seeds, compared median training times, measured MSE, and inspected E-Means' automatic settings.
| Samples | K-Means time | E-Means time | E-Means MSE improvement |
|---|---|---|---|
| 100,000 | 0.1064s | 0.5398s | 3.66% |
| 500,000 | 0.5149s | 1.1484s | 4.26% |
| 1,000,000 | 1.0634s | 2.1533s | 2.55% |
Times are medians across three runs. MSE improvements use the mean MSE across the three runs. K-Means remained faster for all three 2D workloads, while E-Means consistently had lower average MSE. The odd timing pattern from the first run did not hold up. More points alone did not make E-Means faster in these tests.
Experiment 4: More dimensions and clusters
Next I used 128 features and 256 clusters, with 50,000, 100,000, and 200,000 points. I again ran each configuration three times.
| Samples | K-Means time | E-Means time | E-Means speedup | E-Means MSE improvement |
|---|---|---|---|---|
| 50,000 | 3.7166s | 2.1767s | 1.71× | 35.68% |
| 100,000 | 3.5151s | 1.1256s | 3.12× | 34.38% |
| 200,000 | 10.8778s | 2.1408s | 5.08× | 32.63% |
Times are medians across three runs; MSE improvements use mean MSE. E-Means was faster in all three configurations and had substantially lower MSE. The largest observed speedup was about 5.08×, and the 200,000-point run had about 32.63% lower MSE.

This was the most interesting result, but I changed both dimensionality and cluster count from the earlier tests. I cannot attribute the change to dimensionality alone. These were synthetic datasets, so the result does not establish the same gains on real embedding data.
Why did the results change?
Inspecting E-Means' automatic configuration gave me useful context. In the 2D workloads, it selected Quality mode; in the 128D workloads, it selected Throughput mode.
| Samples (2D) | Mode | Batch size | Steps per pass |
|---|---|---|---|
| 100,000 | Quality | 512 | 196 |
| 500,000 | Quality | 2,048 | 245 |
| 1,000,000 | Quality | 4,096 | 245 |
| Samples (128D) | Mode | Batch size | Steps per pass |
|---|---|---|---|
| 50,000 | Throughput | 4,096 | 13 |
| 100,000 | Throughput | 8,192 | 13 |
| 200,000 | Throughput | 16,384 | 13 |
The 128D workload used larger batches and fewer updates per pass. The algorithms also made different numbers of updates and distance calculations. Workload shape and automatic configuration both influenced the comparison; the feature count alone does not explain it. I learned to inspect automatic settings instead of treating an algorithm as a black box.
Experiment 5: Quality vs Throughput
To isolate the mode choice a little, I tested both modes on the same dataset: 200,000 points, two features, and twelve clusters.
| Mode | Training time | MSE | Batch size |
|---|---|---|---|
| Quality | 1.6625s | 4.5151 | 1,024 |
| Throughput | 0.8723s | 4.5155 | 8,192 |
Throughput mode was about 1.9 times faster, with nearly identical MSE. This was a single timing comparison, though, so I would need more repetitions to know how consistent it is. I would not conclude that Throughput mode is always better.
What I learned
- Newer does not always mean faster. K-Means was faster in my first and repeated low-dimensional tests. Performance depends on the workload.
- Dataset size is only one factor. Scaling from 100K to 1M points did not make E-Means faster. Dimensions, cluster count, and training settings mattered too.
- Configuration matters. E-Means selected different batch strategies. Batch size and update count helped explain training time.
- Training time and clustering quality are different metrics. K-Means was faster on the 2D tests, while E-Means achieved lower MSE.
- Repeat benchmarks. The first scaling result was unusual; repeated runs gave a more stable picture. One run is not enough to establish performance.
- Optimization can improve a classical algorithm. EMA updates and automatic batch scheduling apply newer optimization ideas to a familiar clustering objective.
- Read the implementation as well as the paper. Inspecting the selected settings made the benchmark results easier to interpret.
Is E-Means better than K-Means?
In my tests, K-Means was faster on the low-dimensional workloads. E-Means was faster and had lower MSE on the tested high-dimensional workloads. That does not mean E-Means always produces better clusters.
There are several limits to this comparison:
- I used synthetic datasets, not real embeddings.
- The repeated benchmarks had three runs per configuration.
- The algorithms used different initialization procedures and training budgets: up to 25 Lloyd iterations versus five E-Means epochs.
- Timings can vary, and E-Means selected different automatic batch settings across workloads.
- I did not compare them at equal computational cost.
- Some of the MSE difference may come from initialization and training behavior, not only the optimization method.
These experiments show how the two implementations behaved on these workloads. They do not prove a general ranking.
Where could E-Means be useful?
Clustering document embeddings, grouping similar images, organizing large vector datasets, and clustering embeddings for retrieval pipelines are possible use cases. Clustering can also support approximate nearest-neighbor indexing. E-Means does not replace a vector database or vector search; it may improve the clustering part of a system that uses clustering.
I have not benchmarked it on real RAG embeddings or production workloads.
What I want to explore next
- Test E-Means on real document embeddings.
- Compare both algorithms with the same fixed compute budget.
- Benchmark more high-dimensional datasets and random seeds.
- Compare quality and runtime across batch modes.
- Measure how clustering affects approximate nearest-neighbor retrieval.
Closing thoughts
The most interesting part was not just finding which algorithm was faster; it was understanding why the results changed. K-Means was faster on my initial 2D dataset. More 2D points did not change that. With more features and clusters, E-Means became faster and had lower MSE. Inspecting its automatic batch configuration helped me understand the difference.
An algorithm is not better in isolation. Its performance depends on the problem, the data, and how it is implemented.