Get the latest tech news

A not so fast implementation of cosine similarity in C++ and SIMD


There isn’t much to see here, just me dusting off some old architecture knowledge to parallelize computations using SIMD, implement it in C++, and compare the results with Python.

The task involved creating a vectorized version of the cosine similarity in C++ to compare its performance against Python, NumPy, SciPy, and plain C++ implementations. Yes, the vectorized C++ version is an order of magnitude faster, but unless you opt to implement a processor-specific calculation in C++, the Python libraries are decently optimized for tasks like this. Before diving deeper into the difference, I simplified the correlation function by removing extra checks and generalizations that weren’t needed for my use case, making it easier to read and hopefully a bit faster:

Get the Android app

Or read this on Hacker News

Read more on:

Photo of C++

C++

Photo of Cosine Similarity

Cosine Similarity

Photo of SIMD

SIMD

Related news:

News photo

Alternatives to cosine similarity

News photo

Tracking (C++) Shared Pointer Leaks

News photo

Show HN: Iceoryx2 – Fast IPC Library for Rust, C++, and C