VB代码执行效率问题:abs(负数) 与 0-负数,那条语句执行更快?
代码一:Dim lngX As Long
Dim lngY As Long
If lngX<0 then
lngY=Abs(lngX)
Else
lngY=lngX
End if
代码二:
Dim lngX As Long
Dim lngY As Long
lngY=lngX
If lngX<0 then lngY=0-lngX
这两段代码,哪条执行比较快?还是差不多?
Private Sub Command1_Click() Dim t As Double, a As Long, b As Long, i As Long Dim s As String a = -1234 t = Timer For i = 0 To 100000000 b = a If a < 0 Then b = 0 - a Next s = "方法一耗时:" & (Timer - t) a = -1234 t = Timer For i = 0 To 100000000 If a < 0 Then b = Abs(a) Else b = a End If Next s = s & vbCrLf & "方法二耗时:" & (Timer - t) a = -1234 t = Timer For i = 0 To 100000000 If a < 0 Then b = -a Else b = a End If Next s = s & vbCrLf & "方法三耗时:" & (Timer - t) Text1.Text = s End Sub