Which is faster in Python: x**.5 or math.sqrt(x)?

Posted by Casey on Stack Overflow See other posts from Stack Overflow or by Casey
Published on 2008-11-29T01:24:09Z Indexed on 2010/04/23 4:23 UTC
Read the original article Hit count: 447

Filed under:
|

I've been wondering this for some time. As the title say, which is faster, the actual function or simply raising to the half power?

UPDATE

This is not a matter of premature optimization. This is simply a question of how the underlying code actually works. What is the theory of how Python code works?

I sent Guido van Rossum an email cause I really wanted to know the differences in these methods.

My email:

There are at least 3 ways to do a square root in Python: math.sqrt, the '**' operator and pow(x,.5). I'm just curious as to the differences in the implementation of each of these. When it comes to efficiency which is better?

His response:

pow and ** are equivalent; math.sqrt doesn't work for complex numbers, and links to the C sqrt() function. As to which one is faster, I have no idea...

© Stack Overflow or respective owner

Related posts about python

Related posts about Performance