nmslib / hnswlib

Header-only C++/python library for fast approximate nearest neighbors
https://github.com/nmslib/hnswlib
Apache License 2.0
4.15k stars 611 forks source link

How to speed up hnswlib on arm32 platform ? #378

Open SherlockC787 opened 2 years ago

SherlockC787 commented 2 years ago

First of all, thank you for your contribution to the hnswlib library.

I have deployed hnswlib on arm32 (linux 4.19.111 armv7l) using 'searchKnnCloserFirst_test.cpp' as an example.

I modified the original file parameters as follows:

int d = 128;
idx_t n = 5000;
idx_t nq = 10;
size_t k = 1;

I ran the demo on PC (Ubuntu 16.04) and arm32 respectively. I found that the code segment that took the longest time was

for (size_t i = 0; i < n; ++i) {
    // alg_brute->addPoint(data.data() + d * i, i);
        alg_hnsw->addPoint(data.data() + d * i, i); // 800ms on PC; 100+s on arm32
    }

It takes about 800ms on PC and about 100s on arm32, a very big difference.

Before this, I had written a violent matching algorithm for arm32, using text reading to get 5000 data and traversing each data one by one, where loading data took about 2.3s and searching took about 500ms. In terms of time, hnswlib search is faster, about 5ms, however addPoint takes longer, also about 800ms on PC. I would like to ask for advice if there is a way to optimize this part of the time?

Thanks again for your work!

yurymalkov commented 2 years ago

Hi @SherlockC787,

That is very strange. Even bruteforce for n=5000 should be extremely fast, on the sub-millisecond level. Are you sure you have only 5k elements?