Android Question File.ReadString vs File.GetText

max123

Well-Known Member
Licensed User
Longtime User
Hi all,

what's the difference of these ?
B4X:
    Dim myString As String = File.GetText(Dir, FileName)
    Dim myString As String = File.ReadString(Dir, FileName)

Which one to prefer in order to obtain best performances (speed)
in a timing critical context ?

Many thanks
 

Magma

Expert
Licensed User
Longtime User
1651832447658.png


 
Upvote 1

max123

Well-Known Member
Licensed User
Longtime User
Thanks..... Now it is clear to me :D

EDIT: But your answer do not respond to my question. Suppose I do not need to be cross platform, witch is faster?
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Thanks to both. Appreciated

Because I'm courious, now I will look into github opensource B4X to see the code difference.

@agraham you are right, but the code cannot be identical if ReadString is cross platform and GetText no, maybe can be similar.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
but the code cannot be identical if ReadString is cross platform and GetText no, maybe can be similar.

This was an Android question. My reply is correct for Android. Cross-platform does not mean the code is the same on all platforms, just that the function name, parameters and behaviour are the same.

Java:
  public static String GetText(String Dir, String FileName) throws IOException {
    InputStreamWrapper in = OpenInput(Dir, FileName);
    TextReaderWrapper tr = new TextReaderWrapper();
    tr.Initialize((InputStream)in.getObject());
    return tr.ReadAll();
  }

 
  public static String ReadString(String Dir, String FileName) throws IOException {
    InputStreamWrapper in = OpenInput(Dir, FileName);
    TextReaderWrapper tr = new TextReaderWrapper();
    tr.Initialize((InputStream)in.getObject());
    String res = tr.ReadAll();
    in.Close();
    return res;
  }
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Thanks so mutch @agraham , this explain better my question.
As always you are a rock. ?
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Some test on the hand and results.
Most probably this change with device speed....

Loading and reading text of a 5MB HTML file 100 times required 18 seconds, the same for GetText and ReadString, aprox 34 nanoseconds every byte.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
For everyone curious like me, this is a full final inline test code that confirm @agraham is 100% right.
Both GetText and ReadString are pretty the same (tested on Android), B4J only have ReadString.
ReadString, is cross platform and just a bit faster.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
 
    Dim Text As String 'Ignore
    Dim Count As Int = 5000
    Dim Dir As String = File.DirInternal
    Dim FileName As String = "CharSequence_100KB.html"
    Dim Elapsed As Long
 
    File.Copy(File.DirAssets, FileName, Dir, FileName)
 
    Dim FileSize As Long = File.Size(Dir, FileName)
 
    Elapsed = DateTime.Now
    For i = 0 To Count-1
        Text = File.GetText(Dir, FileName)
    Next
    Elapsed = DateTime.Now - Elapsed
 
    Log($"GetText: Read the string of ${FileSize} Bytes ${Count} times required ${Elapsed} millis"$)
    Log($"Aprox ${NumberFormat2((Elapsed/Count)*1000, 0, 0, 3, False)} micros everyone = Aprox $1.2{NumberFormat2((Elapsed/Count)*1000/(FileSize/1000), 0, 0, 3, False)} nanos every byte"$)
 
    '-------------------------------
    
    Elapsed = DateTime.Now
    For i = 0 To Count-1
        Text = File.ReadString(Dir, FileName)
    Next
    Elapsed = DateTime.Now - Elapsed
 
    Log($"ReadString: Read the string of ${FileSize} Bytes ${Count} times required ${Elapsed} millis"$)
    Log($"Aprox ${NumberFormat2((Elapsed/Count)*1000, 0, 0, 3, False)} micros everyone = Aprox $1.2{NumberFormat2((Elapsed/Count)*1000/(FileSize/1000), 0, 0, 3, False)} nanos every byte"$)
 
'    Log(Text)
End Sub

Here my results with a 100KB HTML File:
GetText: Read the string of 101421 Bytes 5000 times required 19230 millis
Aprox 3846.000 micros everyone = Aprox 37.92 nanos every byte
ReadString: Read the string of 101421 Bytes 5000 times required 18748 millis
Aprox 3749.600 micros everyone = Aprox 36.97 nanos every byte
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
So, always because I'm curious, taking on original source code:
Java:
public static String GetText(String Dir, String FileName) throws IOException {
    InputStreamWrapper in = OpenInput(Dir, FileName);
    TextReaderWrapper tr = new TextReaderWrapper();
    tr.Initialize((InputStream)in.getObject());
    return tr.ReadAll();
}

public static String ReadString(String Dir, String FileName) throws IOException {
    InputStreamWrapper in = OpenInput(Dir, FileName);
    TextReaderWrapper tr = new TextReaderWrapper();
    tr.Initialize((InputStream)in.getObject());
    String res = tr.ReadAll();
    in.Close();
    return res;
}
Because ReadString is always a bit faster than GetText ?

It have the same code but 2 more instructions, one instantiate a new String and other close the stream.
Teorically ReadString should be a bit slower, but no.... it is faster.

What is the trick here?
Close the stream ?
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
one instantiate a new String
You can't say that because you don't know how the Java compiler optimises that. As it is only used for the return value, and the return value has to go somewhere the compiler may just optimise out that assignment.

In general you can't just time two different loops, compare then and draw any firm conclusion when the timings are this close. You are running in a multi-threaded garbage collected environment when various other processes, particularly system ones, can stop and start your thread so affecting the elapsed time of your loops. Also the compiler can do non-obvious optimisations that again can affect the results.
 
Upvote 1

max123

Well-Known Member
Licensed User
Longtime User
Many thanks for detailed clarifications.
 
Upvote 0
Top