Python 中哪个更快:x**.5 或 math.sqrt(x)? - Which is faster in Python: x**.5 or math.sqrt(x)?

10/14 03:53
阅读数 0

问题:

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? Python 代码的工作原理是什么?

I sent Guido van Rossum an email cause I really wanted to know the differences in these methods.我给 Guido van Rossum 发了一封电子邮件,因为我真的很想知道这些方法的区别。

My email:我的电子邮件:

There are at least 3 ways to do a square root in Python: math.sqrt, the '**' operator and pow(x,.5).在 Python 中至少有 3 种方法可以计算平方根:math.sqrt、'**' 运算符和 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; pow 和 ** 是等价的; math.sqrt doesn't work for complex numbers, and links to the C sqrt() function. math.sqrt 不适用于复数,并且链接到 C sqrt() 函数。 As to which one is faster, I have no idea...至于哪个更快,我不知道......


解决方案:

参考一: https://en.stackoom.com/question/1N4E
参考二: https://stackoom.com/question/1N4E
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部