为什么** 0.5似乎比sqrt()更有效 [英] Why does **0.5 appear to be more efficient than sqrt()
本文介绍了为什么** 0.5似乎比sqrt()更有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试测量这两种求平方根的速度:
I have tried measuring the speed of these two ways for taking square root:
> system.time(expr = replicate(10000, 1:10000 ** (1/2)))
## user system elapsed
## 0.027 0.001 0.028
> system.time(expr = replicate(10000, sqrt(1:10000)))
## user system elapsed
## 3.722 0.665 4.494
如果sqrt()
功能不能与** 0.5
竞争,为什么我们需要这样的功能?
If the sqrt()
function cannot compete with ** 0.5
, why do we need such a function?
(系统为OS X Yusemite,R版本为3.1.2)
(system is OS X Yusemite, and R version is 3.1.2)
推荐答案
您忘记了重要的括号.以下是纠正该问题后的时间安排:
You forgot important parentheses. Here are the timings after correcting that:
system.time(expr = replicate(10000, (1:10000) ** (1/2)))
#user system elapsed
#4.76 0.32 5.12
system.time(expr = replicate(10000, sqrt(1:10000)))
#user system elapsed
#2.67 0.57 3.31
这篇关于为什么** 0.5似乎比sqrt()更有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文