|
174
|
Programación / Programación Visual Basic / Re: [Reto] IsHour
|
en: 16 Septiembre 2011, 00:43 am
|
@BlackZeroAhh, ok, te lo había copiado de un código tuyo de no sé donde... Viniste a España y no me avisas. Sí, sigo en el grupo, sé que no es muy metal para ti, pero bueno... @raulGracias, corregida... Que listo, justo pruebas con mis funciones lentas... Si probamos con mi función más rápida IsHour_Psyke12 las cosas cambian. Option Explicit Private Declare Sub RtlMoveMemory Lib "kernel32" (ByVal pDst As Long, ByVal pSrc As Long, ByVal ByteLen As Long) Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load() Dim t As New CTiming, c As New Class1, i&, r&, a$(), b As Boolean
Me.AutoRedraw = True a() = Split("34:23:45 5:59:59 10:45:67 raulfeo 00:00:00 14:57:79 111:23:4", " ") t.Reset For i = 0 To 200000 For r = 0 To 6 b = IsHour_r338(a(r)) Next Next Me.Print t.sElapsed, "raul338" Sleep 1000 t.Reset For i = 0 To 200000 For r = 0 To 6 b = isHour_BlackZX(a(r)) Next Next Me.Print t.sElapsed, "Black" Sleep 1000 t.Reset For i = 0 To 200000 For r = 0 To 6 b = c.IsHour_Psyke12(a(r)) Next Next Me.Print t.sElapsed, , "Psyke13"
Set c = Nothing Set t = Nothing End Sub Resultado: DoEvents!
|
|
|
176
|
Programación / Programación Visual Basic / Re: [Reto] IsHour
|
en: 15 Septiembre 2011, 12:49 pm
|
Mi segunda forma de hacerlo (sé que se puede simplificar código con bucles, pero yo lo elijo hacer así )'//En un módulo de clase. Option Explicit Private Declare Sub PutMem4 Lib "msvbvm60.dll" (ByVal Ptr&, ByVal Value&) Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Ptr() As Any) As Long Private lngAscHeader&(5), intAsc%() Public Static Function IsHour_Psyke12(ByRef sHour$) As Boolean If LenB(sHour) = 16 Then lngAscHeader(3) = StrPtr(sHour) '//Check ":" If intAsc(2) = 58 And intAsc(5) = 58 Then '//Hours If intAsc(0) < 48 Or intAsc(0) > 50 Then Exit Function If intAsc(1) < 48 Then Exit Function If intAsc(0) = 50 Then If intAsc(1) > 51 Then Exit Function Else If intAsc(1) > 57 Then Exit Function End If '//Minutes If intAsc(3) < 48 Or intAsc(3) > 53 Then Exit Function If intAsc(4) < 48 Or intAsc(4) > 57 Then Exit Function '//Seconds If intAsc(6) < 48 Or intAsc(6) > 53 Then Exit Function If intAsc(7) < 48 Or intAsc(7) > 57 Then Exit Function IsHour_Psyke12 = True End If End If End Function Private Sub Class_Initialize() lngAscHeader(0) = 1 lngAscHeader(1) = 2 lngAscHeader(4) = &H7FFFFFFF PutMem4 VarPtrArray(intAsc), VarPtr(lngAscHeader(0)) End Sub Private Sub Class_Terminate() PutMem4 VarPtrArray(intAsc), 0 End Sub
Test: Option Explicit Private Sub Form_Load() Dim c As New Class1 Debug.Print String$(50, "=") Debug.Print c.IsHour_Psyke12(Time$) 'True Debug.Print c.IsHour_Psyke12("23:59:59") 'True Debug.Print c.IsHour_Psyke12("00:00:00") 'True Debug.Print c.IsHour_Psyke12("34:54:13") 'False Debug.Print c.IsHour_Psyke12("14:64:24") 'False Debug.Print c.IsHour_Psyke12("22:07:70") 'False Set c = Nothing End Sub
Resultados: ================================================== True True True False False False
EDIT:Mi tercera forma de hacerlo (no creo que sea muy rápida, tan solo doy más opciones... ): '//En un módulo de clase. Option Explicit Private oRegExp As Object Public Static Function IsHour_Psyke13(ByRef sHour$) As Boolean IsHour_Psyke13 = oRegExp.Test(sHour) End Function Private Sub Class_Initialize() Set oRegExp = CreateObject("VBScript.RegExp") With oRegExp .Pattern = "^([01]\d|2[0-3])\:[0-5]\d\:[0-5]\d$" .Global = True End With End Sub Private Sub Class_Terminate() Set oRegExp = Nothing End Sub
Option Explicit Private Sub Form_Load() Dim c As New Class1 Debug.Print String$(50, "=") Debug.Print c.IsHour_Psyke13(Time$) 'True Debug.Print c.IsHour_Psyke13("23:59:59") 'True Debug.Print c.IsHour_Psyke13("00:00:00") 'True Debug.Print c.IsHour_Psyke13("34:54:13") 'False Debug.Print c.IsHour_Psyke13("14:64:24") 'False Debug.Print c.IsHour_Psyke13("22:07:70") 'False Set c = Nothing End Sub
Resultados: ================================================== True True True False False False
EDIT2:Mi cuarta forma de hacerlo: Option Explicit Public Static Function IsHour_Psyke14(ByRef sHour$) As Boolean Dim h As Byte, m As Byte, s As Byte On Error GoTo NoHour: If LenB(sHour) = 16 Then If InStrB(1, ":", MidB$(sHour, 5, 2), vbBinaryCompare) = 0 Then Exit Function If InStrB(1, ":", MidB$(sHour, 11, 2), vbBinaryCompare) = 0 Then Exit Function h = LeftB$(sHour, 4) + 0 If h > 23 Then Exit Function m = MidB$(sHour, 7, 4) + 0 If m > 59 Then Exit Function s = RightB$(sHour, 4) + 0 If s > 59 Then Exit Function IsHour_Psyke14 = True End If Exit Function NoHour: End Function
Test: Option Explicit Private Sub Form_Load() Debug.Print String$(50, "=") Debug.Print IsHour_Psyke14(Time$) 'True Debug.Print IsHour_Psyke14("23:59:59") 'True Debug.Print IsHour_Psyke14("00:00:00") 'True Debug.Print IsHour_Psyke14("34:54:13") 'False Debug.Print IsHour_Psyke14("14:64:24") 'False Debug.Print IsHour_Psyke14("22:04:70") 'False End Sub
Resultados: ================================================== True True True False False False
PD: Lo próximo que tenga que decir haré un comentario nuevo que sino hago una pág kilométrica. DoEvents!
|
|
|
177
|
Programación / Programación Visual Basic / Re: [Reto] IsHour
|
en: 15 Septiembre 2011, 11:05 am
|
Ui que interesante, se me ocurren muchas formas de hacerlo, podré todas las que se me ocurran, con RegExp sería sencillísimo: ^([01]\d|2[0-3])\:[0-5]\d\:[0-5]\d$ (Después lo desarollo antes de que se me adelante raul338) Mi primera forma de hacerlo (dudo que sea la más rápida, pero igual si la más corta): Option Explicit Public Static Function IsHour_Psyke1(ByRef sHour$) As Boolean On Error Resume Next IsHour_Psyke1 = TimeValue(sHour) And (LenB(sHour) = 16) End Function
Test: Private Sub Form_Load() Debug.Print IsHour_Psyke1("12:13:12") Debug.Print IsHour_Psyke1("24:13:12") End Sub
Resultado: DoEvents!
|
|
|
179
|
Programación / Programación Visual Basic / Re: [RETO] IsDate
|
en: 13 Septiembre 2011, 12:55 pm
|
Mi nueva minifunción (no es para ir rápido, pero creo que es la manera más corta de hacerlo): Option Explicit '// Acepta formatos: DD/MM/YYYY, D/MM/YYYY y DD/M/YYYY. Public Static Function IsDate_Psyke12(ByRef sDate$) As Boolean On Error Resume Next IsDate_Psyke12 = InStrB(1, CDate(sDate), sDate, vbBinaryCompare) End Function
La más rápida que se me ocurre: Option Explicit '// Acepta formatos: DD/MM/YYYY, D/MM/YYYY y DD/M/YYYY. Public Static Function IsDate_Psyke13(ByRef sDate$) As Boolean Dim lDay&, lMonth&, lYear&, lp1& On Error GoTo DateError lp1 = InStrB(1, sDate, "/", vbBinaryCompare) If lp1 = 0 Then Exit Function lYear = RightB$(sDate, 8) lDay = LeftB$(sDate, lp1 - 1) If lDay > 31 Then Exit Function If lDay < 1 Then Exit Function lMonth = MidB$(sDate, lp1 + 2, InStrB(lp1 + 1, sDate, "/", vbBinaryCompare) - lp1 - 2) Select Case lMonth Case Is > 12, Is < 1 Exit Function Case 2 If lDay = 29 Then IsDate_Psyke13 = ((lYear Mod &H4 = 0) And (lYear Mod &H64) Or (lYear Mod &H190 = 0)) Exit Function ElseIf lDay > 29 Then Exit Function End If Case Else If lDay = 31 Then Select Case lMonth Case 1,3,5,7,8,10,12 IsDate_Psyke13 = True End Select Exit Function End If End Select IsDate_Psyke13 = True Exit Function DateError: End Function
@Tenient101Quizás un poco larga, pero me gustó la idea, por cierto: Testeo de calidad ============================== 30/07/2000 Tenient101 FAILS 30/12/2011 Tenient101 FAILS 31/12/9999 $Edu$ FAILS Resultados: Testeo de velocidades ============================== 43,271 msec Ignorante v1.1 43,986 msec 79137913 21,627 msec BlackZeroX 60,085 msec $Edu$ 20,118 msec Tenient101 27,267 msec Raul338 18,805 msec Psyke1 29,638 msec Psyke12 12,705 msec Psyke13 23,933 msec IsDate (función original de vb) IsDate() PWND! DoEvents!
|
|
|
|
|
|
|