.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/annoy/plot_s_compile_cpp.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_annoy_plot_s_compile_cpp.py: Compile and run the C++ Annoy precision example. Usage:: >>> python plot_s_compile_cpp.py # default run >>> python plot_s_compile_cpp.py 40 100000 0 # f, n, seed >>> python plot_s_compile_cpp.py --compile-only # only compile Designed so Sphinx can execute it directly using:: >>> python plot_s_compile_cpp.py 10 1000 123 .. GENERATED FROM PYTHON SOURCE LINES 15-115 .. rst-class:: sphx-glr-script-out .. code-block:: none šŸ”Ø Compiling precision_test.cpp ... [CMD] /usr/bin/g++ /home/circleci/repo/galleries/examples/annoy/precision_test.cpp -DANNOYLIB_MULTITHREADED_BUILD -std=c++14 -pthread -o /home/circleci/repo/galleries/examples/annoy/precision_test āœ… Compile done. šŸš€ Running binary... [CMD] /home/circleci/repo/galleries/examples/annoy/precision_test | .. code-block:: Python import contextlib import subprocess import sys import shutil from pathlib import Path # -------------------------------------------------------------- # Paths # -------------------------------------------------------------- # with contextlib.suppress(NameError, TypeError, ValueError): # ROOT = Path(__file__).resolve().parent # else: # ROOT = Path.cwd() try: ROOT = Path(__file__).resolve().parent except NameError: # Sphinx-Gallery fallback ROOT = Path.cwd() CPP_FILE = ROOT / "precision_test.cpp" BIN_FILE = ROOT / "precision_test" # -------------------------------------------------------------- # Helpers # -------------------------------------------------------------- def run_cmd(cmd, **kwargs): """Run a system command and stream output.""" print(f"[CMD] {' '.join(cmd)}") try: subprocess.check_call(cmd, **kwargs) except subprocess.CalledProcessError as e: print("\nāŒ Command failed:", " ".join(cmd)) sys.exit(e.returncode) def compile_cpp(): """Compile the C++ precision binary.""" if not CPP_FILE.exists(): print(f"āŒ C++ file not found: {CPP_FILE}") sys.exit(1) # Detect compiler cxx = shutil.which("g++") or shutil.which("clang++") if not cxx: print("āŒ No C++ compiler (g++/clang++) found in PATH.") sys.exit(1) print("šŸ”Ø Compiling precision_test.cpp ...") cmd = [ cxx, str(CPP_FILE), "-DANNOYLIB_MULTITHREADED_BUILD", "-std=c++14", "-pthread", "-o", str(BIN_FILE) ] run_cmd(cmd) print("āœ… Compile done.\n") def run_binary(args): """Run compiled binary with optional arguments.""" if not BIN_FILE.exists(): print("⚠ No binary found; compiling first...\n") compile_cpp() cmd = [str(BIN_FILE)] + args print("šŸš€ Running binary...\n") run_cmd(cmd) # -------------------------------------------------------------- # Main logic # -------------------------------------------------------------- def main(): args = sys.argv[1:] # Handle flags if "--compile-only" in args: compile_cpp() return # Compile first compile_cpp() # If no arguments → default run if len(args) == 0: run_binary([]) else: run_binary(args) if __name__ == "__main__": with contextlib.suppress(Exception, NameError, TypeError, ValueError): main() .. rst-class:: sphx-glr-timing **Total running time of the script:** (10 minutes 15.809 seconds) .. _sphx_glr_download_auto_examples_annoy_plot_s_compile_cpp.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-plots/scikit-plots/main?urlpath=lab/tree/notebooks/auto_examples/annoy/plot_s_compile_cpp.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/annoy/plot_s_compile_cpp.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_s_compile_cpp.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_s_compile_cpp.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_s_compile_cpp.zip ` .. include:: plot_s_compile_cpp.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_