Android Question Very fast reading a binary file

AlpVir

Well-Known Member
Licensed User
Longtime User
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
 

AlpVir

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could use nio to read the byte buffer as Int(s), but it would read 4 bytes per Int, maybe read it as Short(s) if you just want 2 byte values.
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I also find it surprising that bit shifting is slower than using arithmatic. Very interesting!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…