Unleashing Mojo: Accelerating K-Nearest Neighbor Learning
摘要
New compiled languages such as Mojo, equipped with native SIMD kernels and explicit thread-level parallelism, promise to raise the performance ceiling that pure-Python machine learning (ML) pipelines often hit. We therefore re-implemented brute-force k-nearest neighbours (k-NN) in Mojo—combining 64-byte-aligned buffers, vectorized Euclidean kernels and lock-free thread pools—and benchmarked it against scikit-learn’s canonical Python/Cython implementation. The evaluation spans eleven datasets that vary along three orthogonal axes: sample count ( \(10^{2}\) – \(10^{6}\) ), dimensionality (4–3 072) and structural regularity (MNIST, CIFAR and synthetic blobs). Each experiment was repeated on a laptop-class six-core CPU and a workstation-class sixteen-core CPU to expose hardware effects. Mojo accelerates structured, cache-friendly workloads by five- to ninety-fold and sustains speed-ups of up to 60% on medium-scale image sets even in single-core mode. The margin shrinks to at most 10% on million-point or ultra-wide tables and can invert on low-end laptops when memory bandwidth dominates. These findings show that Mojo is a convenient accelerator for medium-sized, latency-sensitive applications, e.g. recommendation engines and edge analytics—whereas scikit-learn remains the pragmatic choice once datasets outgrow on-chip resources or when ecosystem maturity outweighs raw speed. All code, timing logs, and hardware counters are available in an open repository to facilitate reproduction and further optimization.