Android Question Emulator surpasses the physical device by a factor of 10 or more

AlpVir

Well-Known Member
Licensed User
Longtime User
My app:
1) download from the internet an XML file of about 700 KB
2) creates an new archive SQLite with four table
3) parses the XML file (without using the library XMLSax because I've seen that, for my purposes, it was unnecessary and slowed down the processing of the items)
4) populate the various tables by reading the item from the XML file
5) make some final query by changing the contents of any table.
The app takes 35-40 seconds to do all this in the emulator: an acceptable time !
In physical device vice versa takes from 4 to 10 minutes and even more!
The emulator has the same version of Android (2.3.3. - API Level 10) of the physical device, 512 MB RAM, 32 VM Heap, Internal 200 MB storage, 500 MB SD Card.
The computer has Windows XP SP3, CPU i5 650 3.2 GHz - 2 cores - 4 threads - RAM 4 GB.
Mainboard is Asus P7P55D EVO
The physical device is a smartphone Motorola Defy Plus with internal memory of 2048 MB - SD 32 GB - Class 10.
The time taken from the smartphone to perform the procedures listed above is inexplicably long and actually makes this app unusable.
Obviously I did something wrong or I have not taken account of something else.
I specify that I included in the procedure and not just 4 DoEvents inside loop. There is no timer.
They will be well received suggestions to overcome these serious problems ?
Thank you for your attention.
 

LucaMs

Expert
Licensed User
Longtime User
The problems are not on the emulator, but on the device extremely slow while other apps made by me running with normal speed.
I tried to install the device on both the release version and the release obfuscate.


Sorry for my reply in italian.

Appunto, l'emulatore è molto più lento dei dispositivi reali. L'unico motivo poteva essere il collegamento dispositivo-PC ma se hai installato con la Release, anche questa ipotesi salta.
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
Try writing a log at each point in the app to see exactly where the slowdown occurs. Maybe your physical device is having problems downloading the XML file and most time spent there?
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
First of all I apologize for my bad English.
I started to follow your advice and I have created a kind of report.
Point out that there is no DoEvents statement inside a loop.
Here is the report with a test with the emutatore and 2 with the physical device.

EMULATOR
Download file XML in 2083 ticks
Create Database in 155 ticks
START interpretation in 187 ticks
First read file XML in 5121 ticks
Remove item duplicate in 419 ticks
Reorder in 1 ticks
Second read file XML in 23652 ticks
Registration names teacher in 2852 ticks
Registration names class in 1564 ticks
Simple query executions in 26 ticks
Registration TABLE TabConfiguration in 51 ticks
END interpretation in 22 ticks

DEVICE (FIRS TEST)
Download file XML in 2981 ticks
Create Database in 7574 ticks
START interpretation in 81 ticks
First read file XML in 1346 ticks
Remove item duplicate in 97 ticks
Reorder in 1 ticks
Second read file XML in 112949 ticks
Registration names teacher in 5507 ticks
Registration names class in 5580 ticks
Simple query executions in 20 ticks
Registration TABLE TabConfiguration in 553 ticks
END interpretation in 26 ticks

DEVICE (SECOND TEST)
Download file XML in 1805 ticks
Create Database in 8946 ticks
START interpretation in 97 ticks
First read file XML in 1032 ticks
Remove item duplicate in 111 ticks
Reorder in 3 ticks
Second read file XML in 119552 ticks
Registration names teacher in 5979 ticks
Registration names class in 3052 ticks
Simple query executions in 20 ticks
Registration TABLE TabConfiguration in 575 ticks
END interpretation in 24 ticks

It seems to me that the SQL statements are the responsibility of the slowdown abnormally. For example the statement :

B4X:
    Main.dbOrario.Initialize(File.DirRootExternal, Main.GlobalCartellaOrario & "/" & Main.GlobalNomeDataBase, True)
    Main.dbOrario.ExecNonQuery("DROP TABLE IF EXISTS TabDocenti")
    Main.dbOrario.ExecNonQuery("DROP TABLE IF EXISTS TabClassi")
    Main.dbOrario.ExecNonQuery("DROP TABLE IF EXISTS TabOre")
    Main.dbOrario.ExecNonQuery("DROP TABLE IF EXISTS TabConfig")
    Criterio="CREATE TABLE [TabDocenti] ([IDDocente] INTEGER,[Docente] TEXT(50), TotOreDocente INTEGER)"
    Main.dbOrario.ExecNonQuery(Criterio)
    Criterio="CREATE TABLE [TabClassi] ([IDClasse] INTEGER,[Classe] TEXT(50), [Tipo] TEXT(50), TotOreClasse INTEGER)"
    Main.dbOrario.ExecNonQuery(Criterio)
    Criterio="CREATE TABLE [TabOre] ([ID] INTEGER NOT NULL,[Giorno] INTEGER NOT NULL, [Ora] INTEGER, [Materia] TEXT(50), [Classe] TEXT(50), [Docente] TEXT(50), [Sede] TEXT(50), [Tipo] TEXT(50), [Inizio] FLOAT, [Durata] FLOAT)"
    Main.dbOrario.ExecNonQuery(Criterio)
    Criterio="CREATE TABLE [TabConfig] ([TotClassi] INTEGER,[TotDocenti] INTEGER, [QualeModalitaImportazione] TEXT(2), "
    Criterio= Criterio & "[GlobalNomeDataBase] TEXT(50), [URLProvenienza] TEXT(100), [DataOrario] TEXT(20), [FlagCompresenze] TEXT(1), [ScrittaPulsante1] TEXT(20), [ScrittaPulsante2] TEXT(20),"
    Criterio = Criterio & "    [NomeInsieme1] TEXT(30), [NomeInsieme2] TEXT(30), [IniziOre] TEXT(100),[FlagPulsantiVisibili] TEXT(1), [UltimoDocente] TEXT(50), [UltimaClasse] TEXT(50), [UltimoTipoOrario] TEXT(20),"
    Criterio = Criterio & " [Param1] TEXT(50),[Param2] TEXT(50),[Param3] TEXT(50)"
    Criterio = Criterio & ")"
    Main.dbOrario.ExecNonQuery(Criterio)
    Main.dbOrario.Close

are performed in 155 ticks in the emulator and in 7574 to 8946 ticks in the device. It 'a huge difference !
How do you explain this ?
I use the SQL library vers. 1.2
Thank you all.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I just tested that sub on my Nexus 7 and it is taking about 110 ms.

And slightly less on an emulator, it's a disk intensive process so it could be to do with the speed for the internal memory on your device.

What version of android are you using, and where are you saving the database?
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Does DirRootExternal save to your SDCard? Some devices have a second internal storage area that is accessed via DirRootExternal.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Did you try a single transaction architecture?
I mean something like
begin
drop tab1
drop tab2
crete tab1
create tab2
commit

While you are at it, would you time the drops versus creates time? Long ago I read of really slow drops performed by sqlite.
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
To insert CREATE TABLE and DROP TABLE statements in a BeginTransaction/EndTransaction greatly improves the speed in phase "Create Database". We descend from 7000-8000 ms to about 800 ms. It'is OK.

However, it seems that SQL does not accept a significant number of instructions between BeginTransaction and EndTransaction.

DEVICE (FIRS TEST)
Download file XML in 2981 ticks
Create Database in 7574 ticks
START interpretation in 81 ticks
First read file XML in 1346 ticks
Remove item duplicate in 97 ticks
Reorder in 1 ticks
Second read file XML in 112949 ticks
Registration names teacher in 5507 ticks
Registration names class in 5580 ticks
Simple query executions in 20 ticks
Registration TABLE TabConfiguration in 553 ticks
END interpretation in 26 ticks

In the next phase "Second read XML file" I have about 3000 instructions of type

B4X:
C="INSERT INTO TabOre (ID,Giorno,Ora,Materia,Classe,Docente,Sede,Tipo,Inizio,Durata) "
C=C & "VALUES(" & SQL_IDora & "," & SQL_Giorno & "," & (SQL_Ora+i3-1) & ",'" & SQL_Materia.Replace("0","''") & "','" & SQL_Classe.Replace("0","''") & "','" & SQL_Docente.Replace("'","''").trim & "'"
C=C & ",'" & SQL_Sede.Replace("0","''") & "','" & SQL_Tipo.Replace("0","''") & "'," & SQL_Inizio & "," & SQL_Durata
C=C & ")"
dbOrario.ExecNonQuery(C)

With 10-20 of these instructions the app works and looks fast, but with only 100 the database is literally erased and reset.
At the moment I do not have the opportunity to try a different device but I will certainly soon.
It is inexplicable to me the profound difference between emulator and device operation, in favor of the emulator!
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
Solved !
The number of ticks decreases from 112949 to 4400 ca.
I inserted the code in a critical BeginTransaction / EndTransaction
B4X:
dbOrario.BeginTransaction 
        Try   
            InterpretaOrario
            dbOrario.TransactionSuccessful
        Catch
            Log (LastException.Message)
        End Try
        dbOrario.EndTransaction
The key point was the statement
dbOrario.TransactionSuccessful
Erel to the search for an explanation of the different behavior between emulator and device.
Thank you all.
 
Upvote 0
Top