devxlogo

Use Faster Floating-Point Division

Use Faster Floating-Point Division

If you do a lot of floating-point division operations in VB, you can optimize these operations by multiplying by the reciprocal value. For example, instead of performing this calculation:

 X/Y

Do this:

 X * (1 / Y)

You can see how this works in VB by adding this code to a form in a new project:

 Private Declare Function GetTickCount Lib _	"kernel32" () As LongConst NORMAL As Double = 1453Const RECIPROCAL As Double = 1 / NORMALConst TOTAL_COUNT As Long = 10000000Private Sub Form_Click()	Dim dblRes As Double	Dim lngC As Long	Dim lngStart As Long	On Error GoTo Error_Normal	lngStart = GetTickCount	For lngC = 1 To TOTAL_COUNT		dblRes = Rnd / NORMAL		Next lngC	MsgBox "Normal Time:  " & GetTickCount - lngStart	lngStart = GetTickCount	For lngC = 1 To TOTAL_COUNT		dblRes = Rnd * RECIPROCAL	Next lngC	MsgBox "Reciprocal Time:  " & GetTickCount - lngStartExit SubError_Normal:	MsgBox Err.Number & " - " & Err.DescriptionEnd Sub

I

devx-admin

Share the Post: