Android Question B4XPages very slow running a for next loop with sleep(0)

Enrico Fuoti

Active Member
Licensed User
Longtime User
I tried to convert one of my apps to B4X Pages.
I was very excited. I think b4X Pages is really a great step forward, but I found a big problem in my rearranged application.
Running a Sub that basically inserts records to a local sql-lite db, the performance was really too slow.
For example Using the original standard B4A app 1000 records are inserted in about 20 secs.
Same routine in B4X Pages same inserts are executed in more then 100 secs.
The loop uses sleep(0) to update a progress bar. if i omit sleep(0) the routine executes real fast, but i would not feel very safe to run a for loop without a sleep(0)
Is there anything that can be done to reduce the performance gap ?
 

JohnC

Expert
Licensed User
Longtime User
Maybe reduce the number of times you call sleep(0) in the loop using the MOD operand:
B4X:
Dim L as Long = 0
Do
    L = L + 1   'inc loop count

    .....    'insert one db record

    If L MOD 2 = 0 then   'will run the sleep command 1/2 the time ( L Mod 3 = 1/3 the time, etc)
       Sleep(0)
    end if
Loop
 
Last edited:
Upvote 0

Enrico Fuoti

Active Member
Licensed User
Longtime User
Thank you John,
Your solution improved speed very much. 1000 records got inserted in about 30 secs .. reasonably acceptable..
I didn't think about this... i'll continue to test my app in B4X Pages then :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is exactly zero difference between the performance of a regular app and a "B4XPages app". There is actually no such thing as B4XPages app as B4XPages is just a small library that helps with using panels with a single activity. Nothing special.

Inserting 1000 records shouldn't take more than a few milliseconds. Sounds like you are doing something wrong. Probably not creating a transaction.

Correct thing to do is to remove the Sleep(0) call and use the async batch insert: [B4X] SQL with Wait For
 
Upvote 0

Enrico Fuoti

Active Member
Licensed User
Longtime User
There is exactly zero difference between the performance of a regular app and a "B4XPages app". There is actually no such thing as B4XPages app as B4XPages is just a small library that helps with using panels with a single activity. Nothing special.
of course i believe that, but if is not related to Pages, it my be something in the re-arranged app infrastructure, that may ause the performance difference?
The only reason for I use sleep(0) is to show a progressive percentage of inserted records. without it the single record progress would not be shown.
 
Upvote 0

Enrico Fuoti

Active Member
Licensed User
Longtime User
got it !
actually, the 1000 was only used for timing tests and all were made in release mode.
Records may go up to 7000 or more and i have to make sure that meanwhile a bluetooth reading is not affected by the loop, but without sleep(0) statement it will work anyway. I'll do some tests..
thank you.. :)
 
Upvote 0
Top