Código
Codigo ASM utilizado:
'--------------------------------------------------------------------------------------- ' Module : cFastSqr ' Author : Karcrack ' Now : 22/08/2010 20:25 ' Purpose : Fast alternative for Integer Sqr ' History : 22/08/2010 First cut ......................................................... '--------------------------------------------------------------------------------------- Option Explicit Private sCode As String 'NTDLL Private Declare Sub RtlMoveMemory Lib "NTDLL" (Destination As Any, Source As Any, ByVal Length As Long) Public Function FastSqr(ByVal lValue As Long) As Long ' Filled with ASM later End Function Private Sub Class_Initialize() Dim sCode As String Dim lPtr As Long sCode = ChrW$(&H548B) & ChrW$(&HC24) & ChrW$(&H448B) & ChrW$(&H824) & ChrW$(&HFF2) & ChrW$(&HC02A) & ChrW$(&HFF2) & ChrW$(&HC051) & ChrW$(&HFF2) & ChrW$(&HC02D) & ChrW$(&H289) & ChrW$(&HC031) & ChrW$(&HCC2) & ChrW$(&H0) Call RtlMoveMemory(lPtr, ByVal ObjPtr(Me), 4) Call RtlMoveMemory(ByVal lPtr + &H1C, StrPtr(sCode), 4) End Sub
Código:
http://karcrack.pastebin.com/CLSZPR5d
Para calcular el rendimiento:
Código:
Sub Main()
Dim cFS As New cFastSqr
Dim cT As New CTiming
Dim i As Long
Dim x As Long
cT.Reset
For i = 1 To 10000000
x = cFS.FastSqr(i)
Next i
MsgBox cT.sElapsed
cT.Reset
For i = 1 To 10000000
x = CLng(Sqr(i))
Next i
MsgBox cT.sElapsed
End Sub
Es aproximadamente el doble de rapida en el calculo de Raices enteras
Saludos