本帖最后由 magusneo 于 2012-5-16 12:49 编辑
vbs,ahk_l,au3简单对比下,发现vbs很快而且是快得多,看起来专门处理运算不错
ahk时快时慢波动很大和是否开了其他空程序有关,au3速度较稳定有时比ahk_l快
在我机器上 vbs 59ms,ahk_l从180~360ms,au3 240~280ms
vbsFunction fibonacci(nn)
If nn = 0 Then
fibonacci = 0
Exit Function
End If
If nn <= 2 Then
fibonacci = 1
Exit Function
End If
fibonacci = fibonacci(nn-1) + fibonacci(nn-2)
End Function
testTimes = 20
nn = 23
totalTime = 0
minTime = -1
maxTime = -1
For ii = 0 To testTimes-1
begin = Timer
result = fibonacci(nn)
elapsed = Timer - begin
totalTime = totalTime + elapsed
If elapsed < minTime Or minTime = -1 Then minTime = elapsed
If elapsed > maxTime Then maxTime = elapsed
Next
averageTime = totalTime / testTimes *1000
MsgBox(_
"fibonacci(" & nn & ") =" & vbTab & result & vbCrLf & _
"testTimes:" & vbTab & testTimes & vbCrLf & _
"Average time:" & vbTab & averageTime & vbCrLf & _
"minTime:" & vbTab & minTime*1000 & vbCrLf & _
"maxTime:" & vbTab & maxTime*1000)
ahk_l#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Recommended for catching common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
fibonacci(nn)
{
if ( 0 = nn )
return 0
if ( nn <= 2 )
return 1
return fibonacci(nn-1) + fibonacci(nn-2)
}
nn = 23
testTimes = 20
totalTime = 0
minTime = -1
maxTime = -1
Loop, % testTimes
{
StartTime := A_TickCount
result := fibonacci(nn)
elapsed := A_TickCount - StartTime
totalTime += elapsed
If (elapsed < minTime Or minTime = -1)
minTime := elapsed
If (elapsed > maxTime)
maxTime := elapsed
}
averageTime := totalTime / testTimes
MsgBox, 0, fibonacci, fibonacci(%nn%) = %result%`ntestTimes = %testTimes%`nAverage:%averageTime%`nminTime:%minTime%`nmaxTime:%maxTime%
au3Func fibonacci($nn)
If $nn = 0 Then Return 0
If $nn <= 2 Then Return 1
return fibonacci($nn-1) + fibonacci($nn-2)
EndFunc
$testTimes = 20
$nn = 23
$totalTime = 0
$minTime = -1
$maxTime = -1
For $ii = 0 To $testTimes-1
$begin = TimerInit()
$result = fibonacci($nn)
$elapsed = TimerDiff($begin)
$totalTime += $elapsed
If $elapsed < $minTime Or $minTime = -1 Then $minTime = $elapsed
If $elapsed > $maxTime Then $maxTime = $elapsed
Next
$averageTime = $totalTime / $testTimes
MsgBox(0, "fibonacci", _
"fibonacci(" & $nn & ") =" & @TAB & $result & @CRLF & _
"testTimes:" & @TAB & $testTimes & @CRLF & _
"Average time:" & @TAB & $averageTime & @CRLF & _
"minTime:" & @TAB & $minTime & @CRLF & _
"maxTime:" & @TAB & $maxTime)
|