计算根号2的快慢问题

duckyaya 2003-12-14 07:34:10
试编制一个程序,比较在计算根号2时是用sqr(2)方法快还是用2^(0.5)的方法快,进一步比较二者相差多少倍。

请教中...........
...全文
39 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2003-12-15
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type

Private Declare Function QueryPerformanceCounter Lib "kernel32" _
(lpPerformanceCount As LARGE_INTEGER) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" _
(lpFrequency As LARGE_INTEGER) As Long
Dim starttime As LARGE_INTEGER, frequency As LARGE_INTEGER, nowtime As LARGE_INTEGER
Dim nowtime1 As LARGE_INTEGER
Dim nowtime2 As LARGE_INTEGER
Private Sub Command1_Click()
MsgBox timediff, 0, "needtime(2^0.5)-needtime(sqr(2))"
End Sub
Function timediff()
QueryPerformanceFrequency frequency
QueryPerformanceCounter starttime
a = Sqr(2)
QueryPerformanceCounter nowtime1
b = 2 ^ 0.5
QueryPerformanceCounter nowtime2
timediff = Format((nowtime2.lowpart - nowtime1.lowpart) / frequency.lowpart, "0.00000000000000") & "s"
End Function
回复
boydgmx 2003-12-15
测试表明,很显然sqr快

计算10万次
sqr(2)耗时0.06秒
2 ^ (0.5)耗时0.39秒
回复
飘零风 2003-12-14
sqr快,我觉得这个不用做实验也能肯定。毕竟,sqr()是专用的开平方函数,而^是通用的。
回复
duckyaya 2003-12-14
sqr快!
回复
duckyaya 2003-12-14
Private Sub Form_Load()

Print Timer

For i = 0 To 100000
a = Sqr(2)
Next i

Print Timer

For i = 0 To 100000
a = 2 ^ (0.5)
Next i

Print Timer

End Sub
回复
flyingscv 2003-12-14
应该是sqr(2)
回复
boydgmx 2003-12-14
改天有时间了我试试看,呵呵
回复
duckyaya 2003-12-14
用timer还是?
回复
duckyaya 2003-12-14
怎么计算时间?
回复
flc 2003-12-14
关注
回复
rainstormmaster 2003-12-14
先算出执行10000次sqr(2)所用时间,除以10000
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告