Common mistake when doing practical trigonometry is to compute square roots when that's not necessary:
def cosine_similarity(a, b):
dot_product = sum(x \* y for x, y in zip(a, b))
magnitude_a = sum(x \* x for x in a) \* 0.5 # <- no need for \*0.5
magnitude_b = sum(x \* x for x in b) \* 0.5 # <- no need for \*0.5
return dot_product / (magnitude_a \* magnitude_b)
If you compare your cosines, you might as well compare their squares and avoid costly root computation.
Similarly, in elliptic curve crypto certain expensive operations, such as inversion (x^-1 mod n) are delayed as much as possible down the computation pipeline or even avoided completely when you need to compare two points instead of computing their canonical values.