The following code reads a file 2 bytes at a time and stores the result in an array. It 's all right, but ... there is some other method of reading faster ?
Thanks in advance
B4X:
Dim raf As RandomAccessFile
Dim H As Int
Dim i As Int
Dim Buffer(1201*1201) As Int ' very big array
raf.Initialize2 (File.DirRootExternal , "DEM/3/N44E007.hgt", True, True) ' LittleEndian
Do While raf.CurrentPosition < raf.Size
B1=raf.ReadUnsignedByte(raf.CurrentPosition )
B2=raf.ReadUnsignedByte(raf.CurrentPosition )
H=B1*256+B2
Buffer(N)=H
Log ("B1=" & B1 & " B2=" & B2 & " H=" & H)
N=N+1
If N>10 Then Exit ' debug
Loop
'--- read the buffer
Log ("read the buffer")
For i=0 To 9
Log(Buffer(i))
Next
raf.Close
To read and write bytes, forget the RandomAccessFile library. It is very very slow. Read or write your byte array with a simple InputStream/OutputStream.
Reading with InputStreamToBytes lasts 0.8 sec.
The For-Next loop to rework the result lasts for 43 seconds.
There is some internal function that make the calculations more quickly?
I also feel InputStream but now I feel I have the impression that the sticking point is precisely the elaboration of the results, not the reading.
B4X:
Dim b() As Byte = Bit.InputStreamToBytes(File.OpenInput(File.DirRootExternal , "DEM/3/N44E007.hgt"))
For i=0 To b.Length -1 Step 2
Buffer(N)=b(i)*256+b(i+1)
N=N+1
Next
Slightly faster: reading file 0.192 sec; data processing 43.53 seconds
You can do better ?
Tanks !
B4X:
Dim InputStream1 As InputStream
InputStream1 = File.OpenInput (File.DirRootExternal, "DEM/3/N44E007.hgt")
L=File.Size (File.DirRootExternal, "DEM/3/N44E007.hgt")
Dim b(L) As Byte
count = InputStream1.ReadBytes (b, 0, b.length-1)
For i=0 To b.Length -1 Step 2
Buffer(N)=b(i)*256+b(i+1)
N=N+1
Next
The file is 2818 KB
I've always used the debug mode for this short piece of code.
Moving on to a real device in release mode I got the following times Solution Buffer (N) = b (i) * 256 + b (i + 1) 0.7 sec Solution Buffer (N) = Bit.OR (Bit.ShiftLeft (b (i), 8), b (i + 1)) 0.9 sec
These are times that I think are acceptable, at least in this stage of development of the app that reads and interprets the DEM files
Thanks to all of the aid
I do however finding interesting that a Shift+OR is slower than multiply+add.
I guess modern 32/64-bit processors work in registers of atleast 32-bits so they dont even care if the data is only 8-bits, or they are optimized more for a MAC operation.