calculate the distance between the two endpoints of two vectors a vector is defined as a list tuple or numpy 1d array euclideandistance0 0 2 2 2 8284271247461903 euclideandistancenp array0 0 0 np array2 2 2 3 4641016151377544 euclideandistancenp array1 2 3 4 np array5 6 7 8 8 0 euclideandistance1 2 3 4 5 6 7 8 8 0 calculate the distance between the two endpoints of two vectors without numpy a vector is defined as a list tuple or numpy 1d array euclideandistancenonp0 0 2 2 2 8284271247461903 euclideandistancenonp1 2 3 4 5 6 7 8 8 0 benchmarks noqa up007 noqa up007 calculate the distance between the two endpoints of two vectors a vector is defined as a list tuple or numpy 1d array euclidean_distance 0 0 2 2 2 8284271247461903 euclidean_distance np array 0 0 0 np array 2 2 2 3 4641016151377544 euclidean_distance np array 1 2 3 4 np array 5 6 7 8 8 0 euclidean_distance 1 2 3 4 5 6 7 8 8 0 calculate the distance between the two endpoints of two vectors without numpy a vector is defined as a list tuple or numpy 1d array euclidean_distance_no_np 0 0 2 2 2 8284271247461903 euclidean_distance_no_np 1 2 3 4 5 6 7 8 8 0 benchmarks
from __future__ import annotations

import typing
from collections.abc import Iterable

import numpy as np

Vector = typing.Union[Iterable[float], Iterable[int], np.ndarray]  
VectorOut = typing.Union[np.float64, int, float]  


def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut:
    
    return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2))


def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut:
    
    return sum((v1 - v2) ** 2 for v1, v2 in zip(vector_1, vector_2)) ** (1 / 2)


if __name__ == "__main__":

    def benchmark() -> None:
        
        from timeit import timeit

        print("Without Numpy")
        print(
            timeit(
                "euclidean_distance_no_np([1, 2, 3], [4, 5, 6])",
                number=10000,
                globals=globals(),
            )
        )
        print("With Numpy")
        print(
            timeit(
                "euclidean_distance([1, 2, 3], [4, 5, 6])",
                number=10000,
                globals=globals(),
            )
        )

    benchmark()