Performance of square root calculation - expectation versus reality
07:45 16 Jan 2026

There at least 2 ways to calculate the square root of a number. The most common are (probably):

math.sqrt(x) and x**0.5

On the grounds that the second of these techniques doesn't involve an explicit function call, one might expect it to be faster than the first method. However, that seems not to be the case.

Here's the test code:

"""
Performance comparison between math.sqrt and n ** 0.5
"""

from random import uniform
from math import sqrt
from timeit import timeit
from collections.abc import Iterator

LOOP = 10
URANGE = (0.0, 100.0)

def func1(x: float) -> float:
    """
    Calculate square root using math.sqrt function
    """
    return sqrt(x)

def func2(x: float) -> float:
    """
    Calculate square root using exponential form
    """
    return x**0.5

def pdiff(v1: float, v2: float) -> float:
    """
    Calculate the percentage difference between two values
    """
    return (v1 - v2) / ((v1 + v2) / 2) * 100.0

def rn() -> Iterator[float]:
    """
    Generate LOOP pseudo-random numbers
    """
    for _ in range(LOOP):
        yield uniform(*URANGE)

if __name__ == "__main__":
    N = 5_000_000
    dt = [[], []]
    for ux in rn():
        for a, func in zip(dt, (func1, func2)):
            a.append(timeit(lambda: func(ux), number=N))
            print(func.__name__, f"{a[-1]:.4f}s")
        print()
    p = pdiff(*map(sum, dt))
    print(f"{p:.2f}%")

After multiple runs of this code I observe that math.sqrt is at least 20% faster than the exponent form.

Can anyone explain why this is?

python