Index (cython) python-api benchmark with examples#

An example showing the Index class.

import pytest
import numpy as np
import random; random.seed(0)
from pprint import pprint

# from annoy import Annoy, AnnoyIndex
# from scikitplot.cexternals._annoy import Annoy, AnnoyIndex
# from scikitplot.annoy import Annoy, AnnoyIndex, Index
from scikitplot.annoy._annoy import Index

# print(Index.__doc__)
import subprocess
import sys
import inspect
import joblib
from pathlib import Path

def get_current_script_dir() -> Path:
    """
    Returns the directory of the current script in a robust way.

    Returns
    -------
    Path
        Absolute directory path.

    Raises
    ------
    RuntimeError
        If location cannot be determined.
    """
    # Case 1: normal Python execution
    if "__file__" in globals():
        return Path(__file__).resolve().parent

    # Case 2: fallback for Sphinx-gallery / exec environments
    frame = inspect.currentframe()
    if frame is not None:
        file = inspect.getfile(frame)
        return Path(file).resolve().parent

    raise RuntimeError("Cannot determine script directory.")

BASE_DIR = get_current_script_dir()
PROJECT_ROOT = BASE_DIR.parents[2]

# pytest ../../../scikitplot/annoy/_annoy/tests/test_benchmark_dtype_combinations.py::test_benchmark_summary_table
cmd = [
    sys.executable,
    "-m",
    "pytest",
    # "../../../scikitplot/annoy/_annoy/tests/test_benchmark_dtype_combinations.py::test_benchmark_summary_table",
    "scikitplot/annoy/_annoy/tests/test_benchmark_dtype_combinations.py::test_benchmark_summary_table",
    "-vv",
]

result = subprocess.run(
    cmd,
    cwd=PROJECT_ROOT,
    check=False,
    capture_output=True,
    text=True,
)

print("Return code:", result.returncode)
print(result.stdout)
print(result.stderr)
Return code: 0
============================= test session starts ==============================
platform linux -- Python 3.11.14, pytest-9.0.2, pluggy-1.6.0 -- /root/micromamba/envs/py311/bin/python3.11
cachedir: .pytest_cache
hypothesis profile 'deterministic' -> database=None, deadline=None, print_blob=True, derandomize=True, suppress_health_check=(HealthCheck.data_too_large, HealthCheck.filter_too_much, HealthCheck.too_slow, HealthCheck.large_base_example, HealthCheck.function_scoped_fixture, HealthCheck.differing_executors, HealthCheck.nested_given)
rootdir: /work
configfile: pytest.ini (WARNING: ignoring pytest config in pyproject.toml, setup.cfg!)
plugins: anyio-4.12.1, hypothesis-6.151.9, datadir-1.8.0, regressions-2.10.0, sphinx-0.7.1, asyncio-1.4.0
asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collecting ... collected 1 item

scikitplot/annoy/_annoy/tests/test_benchmark_dtype_combinations.py::test_benchmark_summary_table
index_dtype dtype    operation     n_calls      µs/call     total ms
---------------------------------------------------------------------
int32    float32  add_item         2000        52.20        104.4
int32    float32  build               1     15170.69         15.2
int32    float32  query             500        11.35          5.7
int32    float64  add_item         2000         1.23          2.5
int32    float64  build               1     16244.49         16.2
int32    float64  query             500        19.03          9.5
int64    float32  add_item         2000         0.95          1.9
int64    float32  build               1     20747.10         20.7
int64    float32  query             500        14.18          7.1
int64    float64  add_item         2000         0.98          2.0
int64    float64  build               1     27262.40         27.3
int64    float64  query             500        16.32          8.2
uint64_cpp float32_cpp add_item         2000         1.30          2.6
uint64_cpp float32_cpp build               1     12369.43         12.4
uint64_cpp float32_cpp query             500        10.72          5.4
---------------------------------------------------------------------

Relative overhead vs (int32, float32) baseline:
  int32 /float32 add_item    : 1.000x
  int32 /float32 build       : 1.000x
  int32 /float32 query       : 1.000x
  int32 /float64 add_item    : 0.024x
  int32 /float64 build       : 1.071x
  int32 /float64 query       : 1.676x
  int64 /float32 add_item    : 0.018x
  int64 /float32 build       : 1.368x
  int64 /float32 query       : 1.249x
  int64 /float64 add_item    : 0.019x
  int64 /float64 build       : 1.797x
  int64 /float64 query       : 1.437x
  uint64_cpp/float32_cpp add_item    : 0.025x
  uint64_cpp/float32_cpp build       : 0.815x
  uint64_cpp/float32_cpp query       : 0.945x
PASSED [100%]

============================== 1 passed in 4.41s ===============================

Tags: model-workflow: vector-db level: beginner purpose: showcase

Total running time of the script: (0 minutes 17.570 seconds)

Related examples

Index (cython) python-api with examples

Index (cython) python-api with examples

visualkeras: Vector Index DB

visualkeras: Vector Index DB

annoy.Annoy legacy c-api with examples

annoy.Annoy legacy c-api with examples

annoy.Index python-api with examples

annoy.Index python-api with examples

Gallery generated by Sphinx-Gallery