sqrt(x)和pow(x,0.5)之间的差异 [英] Difference between sqrt(x) and pow(x,0.5)
本文介绍了sqrt(x)和pow(x,0.5)之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道为什么C/c ++中有sqrt()函数,因为我们可以使用
I was wondering why there is sqrt() function in C/c++ as we can achieve the same using
pow(x,0.5);
pow(x,0.5)
的 sqrt(x)
有何不同.有sqrt功能的特定原因吗?
how is sqrt(x)
different for pow(x,0.5)
. Is there a specific reason of having sqrt function?
推荐答案
我为您进行了一项测试,以检查 sqrt(x)
和 pow(x,0.5)
I ran a test for you to check the performance of sqrt(x)
and pow(x,0.5)
1.
for(int i=0;i<100000000;i++)
pow(double(i),0.5);
2.
for(int i=0;i<100000000;i++)
sqrt(double(i));
第一个人花了大约20秒,而第二个人花了大约2秒在我的计算机上.因此,性能会更好.正如其他人已经提到的,可读性是另一个原因.
1st one took around 20 seconds where as 2nd one took around 2 seconds on my computer. So performance is way better. As other have already mentioned readability is other reason.
这篇关于sqrt(x)和pow(x,0.5)之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文