I have to calculate a lot of values in a FOR/NEXT with 8.000.000 loops. It happens each time, when the user press one of 10 buttons. Now it happens, that the user presses the button while the previous loop is still running. In this case I want, that the first loop is given up and restarts with new values. This gives the user a faster running impression.
I tried to enter a Sleep(0) command, so that the buttons can be checked during a loop is running. But this makes the loop extremly slow. so now the loop need 5 times longer than before. this makes my idea obsolet to offer the user a fast speed impression.
I added a 4th way to the code, which brought the result of 100msec! So the problem is solved.
The speed results are:
and on my old Android-4 Tablet:
but on a PC-"BlitzMax"-App
Does anybody know, what I can do to optimize a FOR/NEXT loop or interact with Events during the loop?
Another by-the-way-question: Why do I not need to define the "i" variable in FOR/NEXT? In BlitzMax I need to write:
And what is the scope of this "i"? At the moment I always add a "Dim i as INT" before writing the loop, but I don't know, if this is the best practice or necessary...
I tried to enter a Sleep(0) command, so that the buttons can be checked during a loop is running. But this makes the loop extremly slow. so now the loop need 5 times longer than before. this makes my idea obsolet to offer the user a fast speed impression.
I added a 4th way to the code, which brought the result of 100msec! So the problem is solved.
B4X:
Sub looping
Dim zeit As Long=DateTime.Now
Log("waiting")
Dim i As Int
For i =0 To 8000000
Next
Log("without="& (DateTime.Now-zeit))
zeit =DateTime.Now
For i =0 To 8000000
If i Mod 30000=0 Then
'Sleep(0)
End If
Next
Log("normal="& (DateTime.Now-zeit))
zeit =DateTime.Now
For i =0 To 8000000
If i Mod 30000=0 Then
Sleep(0)
End If
Next
Log("with waiting="& (DateTime.Now-zeit))
zeit =DateTime.Now
For j =0 To 8000000 Step 30000
Sleep(0)
Dim bis As Int=j+30000
If bis>8000000 Then
bis=8000000
End If
For i=j To bis
Next
Next
Log("optimized waiting="& (DateTime.Now-zeit))
End Sub
The speed results are:
without=103msec
normal=150msec
with waiting=274msec
optimized=100msec
and on my old Android-4 Tablet:
without=1183msec
normal=1493msec
with waiting=2278msec
optimized=1103msec
but on a PC-"BlitzMax"-App
without=3msec
normal=37msec
with waiting=40msec
Does anybody know, what I can do to optimize a FOR/NEXT loop or interact with Events during the loop?
Another by-the-way-question: Why do I not need to define the "i" variable in FOR/NEXT? In BlitzMax I need to write:
B4X:
For Local i:INT=0 to 1000
Last edited: