Comparison of ReadAllText vs ReadLine vs ReadBlock vs ReadAllLines

When using ReadAllText method to read a log file with a size of 21.83MB, it takes between  7 seconds (for powerful CPU) to 12 seconds (for weak CPU).

 

SOLUTION SOLVED?
  1. I’ve used all possible method for loading .log/.txt extension file type into TextBox in VB.Net, eg. ReadLine, ReadAllLines, ReadAllText, ReadToEnd, but all methods result are the same. But the time load for the .log file is not actually long when execute in my laptop
  2. I think we should just stick with the current code because every solution that i found on the internet using the same all methods that i just mentioned. Probably will take much more longer time if want to solve this problem.
Failed
StreamReader.ReadLine

Source:

Dim file As New FileInfo("path\to\file")

Using reader As StreamReader = file.OpenText()
    While Not reader.EndOfStream
        Dim nextLine As String = reader.ReadLine()
        ProcessLine(nextLine)
    End While
End Using

 

NO

This method generates error maybe because the memory usage increase overtime due to many lines data read
I’m not sure what cause the error but the detail can be found here:
https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/contextswitchdeadlock-mda

Or maybe because i don’t actually know the correct way to use this method

StreamReader.ReadBlock

Source:

Const MAX_BYTES As Integer = 1048576 * 5 '=10 MB 
Dim bytesRead(MAX_BYTES) As Char
Dim numBytesRead As Integer
Dim currentPos As Integer = 0
Dim strFileName As String
Dim strm As System.IO.Stream
Dim TextLine As String
Dim FileDetail As IO.FileInfo

OpenFileDialogMain.ShowDialog()
strm = OpenFileDialogMain.OpenFile()
strFileName = OpenFileDialogMain.FileName.ToString()
  
Using reader As System.IO.TextReader = _
System.IO.File.OpenText(strFileName)
  numBytesRead = _
reader.ReadBlock(bytesRead, currentPos, MAX_BYTES)
  TextLine = reader.ReadLine()
End Using

txtEditor.Text = New String(bytesRead)
txtEditor.Text += TextLine
txtEditor.Text = txtEditor.Text & vbCrLf & Now

 

NO

This method can be used to fetch .log file but a bit slower than ReadAllText() method.

Completion time to load the log file into textbox is approximately 11-12 seconds

File.ReadAllLines

Source:

NO

This method opens a text file, reads all lines of the file into a string array, and then closes the file.

To load the string array into the textbox will use the same method as ReadLine() which is using Looping

 

Test Methodology

 

TEST DELL-E6410 MSI
StreamReader.Readline

How long does it take for Control Panel to load the log file
“X:\Support\Support Cases\264 (I19-2460-MACRO-ASP)\2020_1_14.log”, File size: 21.83MB

No result because of error No result because of error
StreamReader.ReadBlock

How long does it take for Control Panel to load the log file “X:\Support\Support Cases\264 (I19-2460-MACRO-ASP)\2020_1_14.log”, File size: 21.83MB

11-12 seconds 11-12 seconds
File.ReadAllLines

How long does it take for Control Panel to load the log file “X:\Support\Support Cases\264 (I19-2460-MACRO-ASP)\2020_1_14.log”, File size: 21.83MB

No result because of error No result because of error
What is the CPU? Intel(R) Core(TM) i5 CPU M560 @ 2.67GHz Intel(R) Core(TM) i5-4200 CPU @ 2.50GHz
What is the RAM? 4.00 GB (3.43 usable) 8.00 GB

Conclusion: stick with ReadAllText method to read a log file.