title: How does gensim manage to find the most similar words so fast?
body: <p>Let's say we train a model with more than 1 million words. In order to find the most similar words we need to calculate the distance between the embedding of the test word and embeddings of all the 1 million words words, and then find the nearest words. It seems that Gensim calculate the results very fast. Although when I want to calculate the most similar, my function is extremely slow:</p>
  
  <pre><code>def euclidean_most_similars (model, word, topn = 10):
    distances = {}
    vec1 = model[word]
    for item in model.wv.vocab:
      if item!= node:
        vec2 = model[item]
        dist = np.linalg.norm(vec1 - vec2)
        distances[(node, item)] = dist
    sorted_distances = sorted(distances.items(), key=operator.itemgetter(1))
  </code></pre>
  
  <p>I would like to know how Gensim manages to calculate the most nearest words so fast and what is an efficient way to calculate the most similares.</p>