Results 1 to 9 of 9

Thread: How to improve this

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    How to improve this

    Code:
    Private Sub Timer1_Timer()
     On Error Resume Next
     Static HH As Single
     Static MM As Single
     Static SS As Single
     Static TS As Single
     Static TSS As Single
    
     TS = TS + 1
     
     TSS = TS / 10
     
     If TSS = ".9" Then
       TS = 0
       SS = SS + 1
       
       If SS = "59" Then
         SS = 0
         MM = MM + 1
         
         If MM = "59" Then
           MM = 0
           HH = HH + 1
         End If
       End If
     End If
     
     Label5.Caption = Right("00" & HH, 2) & ":" & _
                      Right("00" & MM, 2) & ":" & _
                      Right("00" & SS, 2) & ":" & _
                      Right("00" & TS, 2)
     
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to improve this

    Try this
    Code:
    Private Sub Timer1_Timer()
    
        Static HH As Long
        Static MM As Long
        Static SS As Long
        Static TS As Long
        
        TS = (TS + 1) Mod 100
        
        If TS = 0 Then
            SS = (SS + 1) Mod 60
            If SS = 0 Then
                MM = (MM + 1) Mod 60
                If MM = 0 Then
                    HH = (HH + 1) Mod 24
                End If
            End If
        End If
    
     
     Label5.Caption = Right("00" & HH, 2) & ":" & _
                      Right("00" & MM, 2) & ":" & _
                      Right("00" & SS, 2) & ":" & _
                      Right("00" & TS, 2)
     
    End Sub
    Last edited by 4x2y; May 17th, 2013 at 06:51 AM. Reason: Change variables declaration to Long instead of Single



  3. #3
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: How to improve this

    And if you really really want to improve it, and have more peformance, remove On Error Resume Next, add CSng() on the lines using Mod wrapping parenthesis around the code. This is on 4x2ys code of course. Also never treat singles as strings. I see you wrapped quotes around the numbers.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to improve this

    @4x2y

    Thanks but I changed TS = (TS + 1) Mod 100 to Mod 10. The last two numbers are in 10th of a second


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to improve this

    I forgot to change variables declaration to Long instead of Single, it is better for speed and also there is no need for single type. There isn't any error can occur from that code, so no need for On Error Resume Next as Jacob Roman said.

    The last two numbers are in 10th of a second
    In this case replace the last line with
    Code:
     Label5.Caption = Right("00" & HH, 2) & ":" & _
                      Right("00" & MM, 2) & ":" & _
                      Right("00" & SS, 2) & ":" & _
                      TS
    Last edited by 4x2y; May 17th, 2013 at 06:58 AM.



  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How to improve this

    Since the Mod operator involves division (even with Integer operands), I therefore believe it is slower than the + operator.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to improve this

    The code without using Mod
    Code:
    Private Sub Timer1_Timer()
    
        Static HH As Long
        Static MM As Long
        Static SS As Long
        Static TS As Long
        
        TS = TS + 1
        
        If TS = 10 Then
            TS = 0
            SS = SS + 1
            If SS = 60 Then
                SS = 0
                MM = MM + 1
                If MM = 60 Then
                    MM = 0
                    HH = HH + 1
                    If HH = 24 Then
                        HH = 0
                    End If
                End If
            End If
        End If
    
     
     Label5.Caption = Right("00" & HH, 2) & ":" & _
                      Right("00" & MM, 2) & ":" & _
                      Right("00" & SS, 2) & ":" & _
                      TS
     
    End Sub



  8. #8
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to improve this

    Double post...



  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How to improve this

    Quote Originally Posted by 4x2y View Post
    The code without using Mod



    Merri once stated that fast code is typically lengthy and I mostly agree with him.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width