Test Foro de elhacker.net SMF 2.1

Programación => .NET (C#, VB.NET, ASP) => Programación General => Programación Visual Basic => Mensaje iniciado por: Psyke1 en 11 Febrero 2011, 14:31 PM

Título: [SRC] IIfEx [by Mr. Frog ©]
Publicado por: Psyke1 en 11 Febrero 2011, 14:31 PM
Bueno, os traigo esta simple función para reemplazar a IIf(). :)
IIf(), es una función muy cómoda de vb, pero no es recomendable usarla en bucles o si se necesita especial agilidad porque es leeeenta. :-(
La mía funciona exactamente igual, con la ventaja de que los argumentos en caso de ser Falso o Verdadero son opcionales. ;)

Código (vb) [Seleccionar]
Option Explicit

Public Static Function IIfEx(ByVal bolExpresion As Boolean, _
                   Optional ByRef varTruePart As Variant, _
                   Optional ByRef varFalsePart As Variant) As Variant
   If bolExpresion Then
       IIfEx = varTruePart
   Else
       IIfEx = varFalsePart
   End If
End Function




Un pequeño ejemplo de velocidad usando CTiming.cls (http://www.xbeat.net/vbspeed/download/CTiming.zip) :

Código (vb) [Seleccionar]
Option Explicit

Private Sub Form_Load()
Dim t               As New CTiming
Dim x               As Long
Dim ret             As Variant
Const s             As String = "holaa"
Const sCorrect      As String = s
Const sIncorrect    As String = sCorrect & "a"
Const lngLoops      As Long = 100000

   If App.LogMode = 0 Then
       MsgBox "Compile it stupid!", vbCritical
       End
   End If

   Me.AutoRedraw = True
   
   Me.Print "True part"
   Me.Print
   
   t.Reset
   For x = 1 To lngLoops
       ret = IIf((s = sCorrect), 123, 1233)
   Next
   Me.Print "IIf", t.sElapsed
   
   t.Reset
   For x = 1 To lngLoops
       ret = IIfEx((s = sCorrect), 123, 1233)
   Next
   Me.Print "IIfEx", t.sElapsed
   
   Me.Print String$(20, "-")
   Me.Print "False part"
   Me.Print
   
   t.Reset
   For x = 1 To lngLoops
       ret = IIf((s = sIncorrect), 123, 1233)
   Next
   Me.Print "IIf", t.sElapsed
   
   t.Reset
   For x = 1 To lngLoops
       ret = IIfEx((s = sIncorrect), 123, 1233)
   Next
   Me.Print "IIfEx", t.sElapsed
End Sub


Resultado (IIfEx = IIIf ; que le cambié el nombre :rolleyes:) :
(http://img109.imageshack.us/img109/1305/dibujobby.jpg)

(http://images1.memegenerator.net/ImageMacro/5008584/Problem.jpg?imageSize=Large&generatorName=Troll-Face)

Nota: Aún así si se necesita especial velocidad mejor usar If.  :rolleyes:

DoEvents! :P
Título: Re: [SRC] IIIf [by Mr. Frog ©]
Publicado por: 79137913 en 11 Febrero 2011, 14:35 PM
HOLA!!!

Muy buena Mr Frog!!!

Me hacen recordar al SI(condicion,ValorVerdadero,ValorFalso) del EXCEL.

Igual ya te lo habia dicho :P.

Re bien, Mejora en un 40% a la iif Original, esta perfecta!

GRACIAS POR LEER!!!
Título: Re: [SRC] IIfEx [by Mr. Frog ©]
Publicado por: Psyke1 en 11 Febrero 2011, 15:15 PM
Gracias, unicamente quería demostrar que para mejorar la velocidad de vb no hace falta romperse la cabeza.

Código (vb) [Seleccionar]

' Hago un poco de trampa usando otra función de vb, solo es una adaptación para usar MidB() como Mid()... :P
Public Static Function fMid(ByRef sText As String, ByVal lngStart As Long, Optional ByVal lngLength As Long) As String
   fMid = MidB$(sText, 1 + lngStart + lngStart, lngLength + lngLength)
End Function


Código (vb) [Seleccionar]
Option Explicit

Private Sub Form_Load()
Dim t               As New CTiming
Dim x               As Long
Dim ret             As String
Dim s               As String
Const lngLoops      As Long = 100000
Const lngStart      As Long = 34566
Const lngLen        As Long = 10000

   If App.LogMode = 0 Then
       MsgBox "Compile it stupid!", vbCritical
       End
   End If
   
   Show
   AutoRedraw = True
   
   For x = 0 To 100000
       s = s & ChrW$(Rnd * 255)
   Next
   
   Cls
   
   t.Reset
   For x = 1 To lngLoops
       ret = fMid(s, lngStart, lngLen)
   Next
   Print "fMid", t.sElapsed
   
   ret = vbNullString
   
   t.Reset
   For x = 1 To lngLoops
       ret = Mid$(s, lngStart, lngLen)
   Next
   Print "Mid", t.sElapsed
End Sub


Resultado:
(http://img842.imageshack.us/img842/8358/dibujoowi.jpg)

DoEvents! :P