Level: Beginner
This snippet does not use a b4a Timer. It uses DateTime.Now to get ticks to show the length of time in milliseconds a set of Subs and/or algorithms take to complete. Lines 1 - 5 are all you need to add to your code to profile it for bottle-necks.
Add global declaration so you can use the time variables anywhere
Surround your code to test with Line 2 before it and Lines 3-5 after it.
The complete sample program is below. I put it in the Main activity.
Tags: DateTime, milliseconds, ticks, bottle-necks
This snippet does not use a b4a Timer. It uses DateTime.Now to get ticks to show the length of time in milliseconds a set of Subs and/or algorithms take to complete. Lines 1 - 5 are all you need to add to your code to profile it for bottle-necks.
Add global declaration so you can use the time variables anywhere
B4X:
Sub Process_Globals
'Line 1
Public StartTime, EndTime, MilliSeconds As Long
End Sub
Surround your code to test with Line 2 before it and Lines 3-5 after it.
B4X:
'Line 2
StartTime = DateTime.Now
'Sub(s) or algorithm(s) to test
ProcessToTest(1000,0,0) 'one second
ProcessToTest(2000,0,0) 'two seconds
ProcessToTest(3000,0,0) 'three seconds
'Lines 3, 4, 5
EndTime = DateTime.Now
MilliSeconds = EndTime - StartTime
Log("Total Time: " & MilliSeconds)
The complete sample program is below. I put it in the Main activity.
B4X:
Sub Process_Globals
'Line 1
Public StartTime, EndTime, MilliSeconds As Long
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
End Sub
Sub Activity_Resume
Dim Button1 As Button
Button1.Initialize("Button1")
Button1.Text = "TimeIt"
Activity.AddView(Button1,0%x,0%y,25%x,10%y)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Button1_Click
TimeIt
End Sub
Sub TimeIt
'Line 2
StartTime = DateTime.Now
'Sub(s) or algorithm(s) to test
ProcessToTest(1000,0,0) 'one second
ProcessToTest(2000,0,0) 'two seconds
ProcessToTest(3000,0,0) 'three seconds
'Lines 3, 4, 5
EndTime = DateTime.Now
MilliSeconds = EndTime - StartTime
Log("Total Time: " & MilliSeconds)
End Sub
Sub ProcessToTest(ms As Long, start As Long, stop As Long)
Dim elapsed_ms = 0 As Long
start = DateTime.Now
Do While elapsed_ms < ms
stop = DateTime.Now
elapsed_ms = stop - start
Loop
End Sub
Tags: DateTime, milliseconds, ticks, bottle-necks