Android Question Can this code be improved?

D

Deleted member 103

Guest
Hi,

This code is meant to be a countdown timer that should trigger a beep every full second.
I've tried a "timer" and a "do while" loop, but the performance is not so good in both.

The beep has a delay of up to 2 hundredths of a second.
Is there a way to reduce this delay?

BeepTimer.JPG
 

Attachments

  • Beep-Example.zip
    8.4 KB · Views: 247

Emme Developer

Well-Known Member
Licensed User
Longtime User
Maybe this can be a solution?
You need to adapt the code to show milleseconds and the log with timer
 

Attachments

  • try.zip
    7 KB · Views: 206
Upvote 0
D

Deleted member 103

Guest
What is the interval of the timer?
B4X:
    beeptimer.Initialize("beeptimer", 5)

Here is my new example with the Threading-Library, it looks much better.

Code:
B4X:
Sub Run_BeepThread
    Dim arg(0) As Object
    Dim obj(1) As Object

    Do While IsBeepEnabled
        IntervalMSec = lgTime - (DateTime.Now - StartTickCount)

        obj(0) = IntervalMSec
        beepThread.RunOnGuiThread("Update_ClockLabel",obj)
    
        If IntervalMSec <= restzeit Then
            If restzeit > 0 Then
                Log("Thread-IntervalMSec= " & IntervalMSec)

                beepThread.RunOnGuiThread("Start_beep200", arg)
            Else If restzeit = 0 Then
                beepThread.RunOnGuiThread("Start_beep500", arg)

                beepThread.RunOnGuiThread("btnStop_Click",arg)
            End If
    
            Dim rest As Long = (restzeit - IntervalMSec) / 1000
            'Log("rest=" & rest)
            If rest > 1 Then
                restzeit = restzeit - (rest * 1000 + 1000)
            Else
                restzeit = restzeit - 1000
            End If
        End If
        beepThread.Sleep(5)
    Loop
End Sub

Logs:
B4X:
Thread-IntervalMSec= 9998
b200.Beep
Thread-IntervalMSec= 8998
b200.Beep
Thread-IntervalMSec= 7999
b200.Beep
Thread-IntervalMSec= 6998
b200.Beep
Thread-IntervalMSec= 6000
b200.Beep
Thread-IntervalMSec= 4996
b200.Beep
Thread-IntervalMSec= 4000
b200.Beep
Thread-IntervalMSec= 2998
b200.Beep
Thread-IntervalMSec= 1996
b200.Beep
Thread-IntervalMSec= 996
b200.Beep
b500.Beep
 
Timer-IntervalMSec= 9979
b200.Beep
Timer-IntervalMSec= 8989
b200.Beep
Timer-IntervalMSec= 7998
b200.Beep
Timer-IntervalMSec= 6989
b200.Beep
Timer-IntervalMSec= 5988
b200.Beep
Timer-IntervalMSec= 4993
b200.Beep
Timer-IntervalMSec= 3995
b200.Beep
Timer-IntervalMSec= 2998
b200.Beep
Timer-IntervalMSec= 1982
b200.Beep
Timer-IntervalMSec= 988
b200.Beep
b500.Beep
 

Attachments

  • Beep-Example_2.zip
    8.8 KB · Views: 202
Upvote 0

ilan

Expert
Licensed User
Longtime User
Why dont u use interval of 1 sec (1000ms) and then make the beep instead of checking every 5 ms if 1 second has past?

Most phone will not give u an interval of 5ms!!
 
Upvote 0
D

Deleted member 103

Guest
Why dont u use interval of 1 sec (1000ms) and then make the beep instead of checking every 5 ms if 1 second has past?

Most phone will not give u an interval of 5ms!!
It's not because I need to update a label with ms in the timer.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
It's not because I need to update a label with ms in the timer.

I dont know what exactly u r doing but using a timer with 5ms is not the right way.

If it is very very very important for u to get every x time the beep u could use a game engine that uses the phone hardware. it will give u a better result and u can use it also for your label animation instead of a timer. Try acclerated surface.

A timer with 5ms will not really update every 5ms at least not on all phones or even on most phones.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I am now at work but when i get to my pc i will look at your example and see if i can get a better result. Does your example also include the label update function?

Do u really need 5ms for the update? U know that 16ms means 60 frames/sec what means that all games runs on 60 frames per sec and thats smooth enough for the human eye, unless u r targeting also flies :rolleyes:
 
Last edited:
Upvote 0
D

Deleted member 103

Guest
Hi ilan,

Thank you for your example. :)
I have tested it and as you can see from my screenshot, I have a very different result. :(

Do u really need 5ms for the update? U know that 16ms means 60 frames/sec what means that all games runs on 60 frames per sec and thats smooth enough for the human eye, unless u r targeting also flies :rolleyes:
What I need is a routine that makes me a beep at every full second (+ - 5ms).
The label does not have to be updated every 5ms, so it is not necessary.


beepexample.PNG
 
Last edited by a moderator:
Upvote 0

ilan

Expert
Licensed User
Longtime User
Try it several times

You can see that 10026 ms past from the test start what means it is a very good result.

Dont forget i set the engine to 16 frames but 1000 ms has 16.66666 (if you count 60 fps)

So this is why u get a small difference but at the end it is precise enough for your needs.

Make sure you try it in RELEASE MODE !!!
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
What I need is a routine that makes me a beep at every full second (+ - 5ms).

as i said before, i would use for that a simple timer with interval 1000 or the sleep(1000) function and changed the label to show "HH:mm:ss" and like that update it every 1 seconds instead of showing also ms.

anyway the AC example is also good enough for your needs. you can see that from start to end it took 10026 ms in your phone so 26ms delay for 10 sec is 2.6ms for each sec in average so it is inside the +/- 5ms

but i would still use a simple timer for that with 1000 ms interval if you just want to have a beep after 1 sec.

you can also try nanotime library where you can get a much precise time (nano seconds!!!)

1 ms = 1000000 nano seconds :D
 
Upvote 0
D

Deleted member 103

Guest
anyway the AC example is also good enough for your needs. you can see that from start to end it took 10026 ms in your phone so 26ms delay for 10 sec is 2.6ms for each sec in average so it is inside the +/- 5ms
It's true I have an average of 5ms at 10 seconds,
but these 5ms must not be the average.

It needs this tolerance are respected at every second.
My app should be an accurate timer for oldtime race.

After my test, the threading library gives the best results.:)
But Erel says that you should stop using this library. :(
 
Upvote 0
D

Deleted member 103

Guest
Try it several times
I did, but it does not get better.
Logger connected to: HT632BE01102
--------- beginning of crash
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
b200.Beep
8998
b200.Beep
7989
b200.Beep
6798
b200.Beep
5582
b200.Beep
4354
b200.Beep
3136
b200.Beep
1921
b200.Beep
705
b500.Beep
This task took: 10022 ms
b200.Beep
8974
b200.Beep
7965
b200.Beep
6914
b200.Beep
5697
b200.Beep
4603
b200.Beep
3594
b200.Beep
2580
b200.Beep
1362
b200.Beep
133
b500.Beep
This task took: 10030 ms
b200.Beep
8995
b200.Beep
7987
b200.Beep
6830
b200.Beep
5614
b200.Beep
4400
b200.Beep
3181
b200.Beep
1963
b200.Beep
734
b500.Beep
This task took: 10038 ms
b200.Beep
8995
b200.Beep
7985
b200.Beep
6976
b200.Beep
5969
b200.Beep
4961
b200.Beep
3951
b200.Beep
2943
b200.Beep
1935
b200.Beep
824
b500.Beep
This task took: 10028 ms
b200.Beep
9011
b200.Beep
8001
b200.Beep
6985
b200.Beep
5765
b200.Beep
4542
b200.Beep
3316
b200.Beep
2094
b200.Beep
879
b500.Beep
This task took: 10040 ms
b200.Beep
8997
b200.Beep
7988
b200.Beep
6977
b200.Beep
5763
b200.Beep
4545
b200.Beep
3328
b200.Beep
2112
b200.Beep
880
b500.Beep
This task took: 10033 ms
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
It's true I have an average of 5ms at 10 seconds,
but these 5ms must not be the average.

It needs this tolerance are respected at every second.
My app should be an accurate timer for oldtime race.

After my test, the threading library gives the best results.:)
But Erel says that you should stop using this library. :(

it will depend on what phone your app is running, i doubt that it will run the same on all phones.
but i also doubt that there is a 100% countdown app some where so 5ms is really not that bad filippo.

How Fast Is a Blink of an Eye? The average duration for a single blink of a human eye is 0.1 to 0.4 seconds, or 100 to 400 milliseconds, according to the Harvard Database of Useful Biological Numbers.Apr 24, 2017

100 to 400 ms for blinking and you are struggling about 5ms ?! o_O
 
Upvote 0
D

Deleted member 103

Guest
100 to 400 ms for blinking and you are struggling about 5ms ?! o_O
The people who use my app usually use such instruments, and expect my app to do about the same.
Each beep must come exactly on the hundredth of a second.

These old-time races are about the accuracy and not the speed.
There are penalty points for every hundredth of a second.
 
Upvote 0
D

Deleted member 103

Guest
Well, with this trick I have now improved my code. :)
All you have to do is update the label "lblClock" every 100ms and the world looks completely different. :D
The timer now gives just as good results as the threading library. ;)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Timer-IntervalMSec= 9999
b200.Beep
Timer-IntervalMSec= 8999
b200.Beep
Timer-IntervalMSec= 7999
b200.Beep
Timer-IntervalMSec= 6997
b200.Beep
Timer-IntervalMSec= 5999
b200.Beep
Timer-IntervalMSec= 4997
b200.Beep
Timer-IntervalMSec= 3999
b200.Beep
Timer-IntervalMSec= 2997
b200.Beep
Timer-IntervalMSec= 1996
b200.Beep
Timer-IntervalMSec= 997
b200.Beep
b500.Beep
This task took: 10004 ms
Thread-IntervalMSec= 9998
b200.Beep
Thread-IntervalMSec= 8999
b200.Beep
Thread-IntervalMSec= 7999
b200.Beep
Thread-IntervalMSec= 6997
b200.Beep
Thread-IntervalMSec= 5999
b200.Beep
Thread-IntervalMSec= 4997
b200.Beep
Thread-IntervalMSec= 3999
b200.Beep
Thread-IntervalMSec= 2997
b200.Beep
Thread-IntervalMSec= 2000
b200.Beep
Thread-IntervalMSec= 999
b200.Beep
b500.Beep
This task took: 10002 ms
** Service (smhomeclock) Start **
 

Attachments

  • Beep-Example_3.zip
    8.9 KB · Views: 200
Upvote 0
Top