|
-
May 16th, 2013, 05:30 PM
#1
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.
-
May 16th, 2013, 06:52 PM
#2
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
-
May 16th, 2013, 07:06 PM
#3
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.
-
May 16th, 2013, 07:39 PM
#4
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.
-
May 17th, 2013, 06:42 AM
#5
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.
-
May 17th, 2013, 06:56 AM
#6
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)
-
May 17th, 2013, 07:12 AM
#7
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
-
May 17th, 2013, 07:13 AM
#8
-
May 17th, 2013, 07:24 AM
#9
Re: How to improve this
 Originally Posted by 4x2y
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|