Read Filex Text by: (UTF8 IS FAST than StrConv)
StrConv(bytes,vbUnicode): 452 ms
Utf8text FileRead:286MS
read unicode text :s=block() as byte 170.6ms
read unicode text by Pointer: 117ms
'====================
if save string to text with unicode format,so no need StrConv(Block, vbUnicode)
Read unicode Txt file is fast than StrConv(block, vbUnicode) 126%
The space occupied by the hard disk is doubled, and the operating speed is also doubled. The speed of NVE and M2 solid-state hard disks can be increased even more. Programs to run fast, hard disk reads and writes fast, CPU is powerful, and memory is high speed, all of which can add points
The SPLIT function of vb6 takes 23 seconds (23000ms)
Fast SPLIT algorithm 660 ms
Analog pointer method takes 206 milliseconds
vb6 openfile+Split =458+29048 (=29.5 sec) 29500
:wave:DownLoad Sample test:Split_EnglishTest.zip
![Name: SplitTestDemo.jpg
Views: 120
Size: 27.5 KB]()
By using the pointer binding method, the speed is increased by 200 times.
quick split is 132% faster than line input
Line Input from Txt File :3405.335 ms(str lines=3417225)
Loading time from pointer file to string array: 128.8862 ms
【25 times faster= 2500%】
If you use pointers, you don't need to have MidB$ for each line of string, will it be faster?
【Treat a super long string as a binary data address, and then bind it to a virtual string array, so that there is no need to copy the string multiple times to achieve a speed-up method. The larger the amount of data, the faster the speed.
Change the 4 bytes of the line break to the length of this line, and then bind the address of each line to the array pointer】
If the file keeps increasing data, only the newly added content is read each time, and certain bytes can be skipped to speed up the reading speed. You can also add data to the software and read another software, using memory mapping technology, the speed will be faster, no need to save on the hard disk
STUDY on StreamReader in VB6?-VBForums
https://www.vbforums.com/showthread....mReader-in-VB6
StrConv(bytes,vbUnicode): 452 ms
Utf8text FileRead:286MS
read unicode text :s=block() as byte 170.6ms
read unicode text by Pointer: 117ms
Code:
New_c.Timing True
F = FreeFile(0) '822
Open FILE_TO_SPLIT For Binary Access Read As #F
FileLenA = LOF(F)
ReDim block(FileLenA - 1)
Get #F, , block
Close #F
Str = StrConv(block, vbUnicode)
Print "StrConv(bytes,vbUnicode) GET FILE TEXT:" & New_c.Timing
'===================
New_c.Timing True
F = FreeFile(0) '822
Open "Utf8text.txt" For Binary Access Read As #F
FileLenA = LOF(F)
ReDim block(FileLenA - 4)
Get #F, 4, block
Close #F
Str = Utf8PtrToUnicode(VarPtr(block(0)))
Print "Utf8text FileRead:" & New_c.Timing
ReDim block(0)
'MsgBox Str2 = Str
Str = ""
'==================
New_c.Timing True
Dim fnum As Integer '
fnum = FreeFile
Open "UnicodeText1.txt" For Binary As #fnum
ReDim block(LOF(fnum) - 3) As Byte
Get #1, 3, block
Str = block
Print "unicode GET TEXT:" & New_c.Timing
Code:
'read unicode text by Pointer: 117ms
fnum = FreeFile
Open "UnicodeText2.txt" For Binary As #fnum
Dim StrLen1 As Long
StrLen1 = LOF(fnum) - 4
ReDim block(LOF(fnum) - 1) As Byte
'前面4个字节无效(第一个字符留空),作为字符长度
Get #1, , block()
'Str = block
'Str = String(StrLen1 / 2, 0)
Str = StringHelpers.SysAllocStringLen(ByVal 0&, StrLen1 / 2) '
'以前测试结果可以提速 47.65%
'Str = String(StrLen1, vbNullChar)
Dim lTmp As Long, lTmp2 As Long
'CopyMemory lTmp, ByVal VarPtr(Str), 4
Call AuxVBvm.GetMem4(ByVal VarPtr(Str), lTmp)
'CopyMemory block(0), StrLen1, 4
Call AuxVBvm.PutMem4(VarPtr(block(0)), StrLen1)
'CopyMemory ByVal VarPtr(Str), VarPtr(block(4)), 4
Call AuxVBvm.PutMem4(ByVal VarPtr(Str), VarPtr(block(4)))
Print "Unicode文件字节指针到字符串:" & New_c.Timing & "," & Len(Str)
'MsgBox "读unicode得到文件内容:" & Str
CopyMemory ByVal VarPtr(Str), lTmp, 4
Erase block()
Close #fnum
if save string to text with unicode format,so no need StrConv(Block, vbUnicode)
Read unicode Txt file is fast than StrConv(block, vbUnicode) 126%
The space occupied by the hard disk is doubled, and the operating speed is also doubled. The speed of NVE and M2 solid-state hard disks can be increased even more. Programs to run fast, hard disk reads and writes fast, CPU is powerful, and memory is high speed, all of which can add points
Code:
Dim S As String
Dim Bt() As Byte
Bt = OpenBinFile2(App.Path & "\UNICODE.txt", 2)
S = Bt
Function OpenBinFile2(filename As String, Optional SeekSize As Long, Optional ErrInfo As String) As Byte()
'[mycode_id:1903],edittime:2011/7/11 13:27:34
On Error Resume Next
Dim hFile As Integer
hFile = FreeFile
Open filename For Binary As #hFile
If SeekSize > 0 Then
Seek #hFile, SeekSize + 1
ReDim OpenBinFile2(LOF(hFile) - 1 - SeekSize)
Else
ReDim OpenBinFile2(LOF(hFile) - 1)
End If
Get #hFile, , OpenBinFile2
Close #hFile
End Function
The SPLIT function of vb6 takes 23 seconds (23000ms)
Fast SPLIT algorithm 660 ms
Analog pointer method takes 206 milliseconds
vb6 openfile+Split =458+29048 (=29.5 sec) 29500
:wave:DownLoad Sample test:Split_EnglishTest.zip
By using the pointer binding method, the speed is increased by 200 times.
quick split is 132% faster than line input
Line Input from Txt File :3405.335 ms(str lines=3417225)
Loading time from pointer file to string array: 128.8862 ms
【25 times faster= 2500%】
If you use pointers, you don't need to have MidB$ for each line of string, will it be faster?
【Treat a super long string as a binary data address, and then bind it to a virtual string array, so that there is no need to copy the string multiple times to achieve a speed-up method. The larger the amount of data, the faster the speed.
Change the 4 bytes of the line break to the length of this line, and then bind the address of each line to the array pointer】
If the file keeps increasing data, only the newly added content is read each time, and certain bytes can be skipped to speed up the reading speed. You can also add data to the software and read another software, using memory mapping technology, the speed will be faster, no need to save on the hard disk
STUDY on StreamReader in VB6?-VBForums
https://www.vbforums.com/showthread....mReader-in-VB6
Code:
Dim File1 As String
Dim FileSizeA As Long
Dim DataArr() As String
Private Sub Command1_Click()
QuickSplit_File2 File1, vbCrLf, DataArr(), , FileSizeA
End Sub
Private Sub Command2_Click()
Dim DataSize As Long
Dim StartPos As Long
StartPos = FileSizeA
'Get NewStr,Get the newly added content of the notepad file to the string array
QuickSplit_File2 File1, vbCrLf, DataArr(), StartPos, FileSizeA, DataSize
End Sub
Code:
Private Declare Function SafeArrayRedim Lib "oleaut32" (ByVal saPtr As Long, saBound As Long) As Long
Public Sub QuickSplit_File(File1 As String, Delimiter As String, ResultSplit() As String)
'比QuickInput_File快132%
Dim Str As String
Dim Remaining As Long, F As Long, Block() As Byte
F = FreeFile(0)
Open File1 For Binary Access Read As #F
Remaining = LOF(F)
ReDim Block(Remaining - 1)
Get #F, , Block
Close #F
Str = StrConv(Block, vbUnicode)
Dim lngA As Long, lngCount As Long, lngDelLen As Long, lngExpLen As Long, lngResults() As Long
' some dummy variables that we happen to need
Dim Compare As VbCompareMethod, SafeArrayBound(1) As Long
' length information
lngExpLen = LenB(Str)
lngDelLen = LenB(Delimiter)
' validate lengths and limit (limit must be larger than 0 or it must be unlimited)
If lngExpLen > 0 And lngDelLen > 0 Then
' now look up for the first position
lngA = InStrB(1, Str, Delimiter, Compare)
' InStrB is very fast, but it may give "between characters" results
Do Until (lngA And 1) Or (lngA = 0)
' this is why we look for odd positions (1, 3, 5, 7 etc. are a valid position)
lngA = InStrB(lngA + 1, Str, Delimiter, Compare)
Loop
'------------------
' unlimited, reserve space for maximum possible amount of returned items
ReDim lngResults(0 To (lngExpLen \ lngDelLen))
' index positions until none is found
Do While lngA > 0
' remember this position
lngResults(lngCount) = lngA
' look for the next one
lngA = InStrB(lngA + lngDelLen, Str, Delimiter, Compare)
Do Until (lngA And 1) Or (lngA = 0)
lngA = InStrB(lngA + 1, Str, Delimiter, Compare)
Loop
' increase found counter
lngCount = lngCount + 1
Loop
'-----------------
' set results to actual findings
ReDim Preserve ResultSplit(0 To lngCount)
' see if we found any results
If lngCount = 0 Then
' nope, just set the only item to be the whole string
ResultSplit(0) = Str
Else
' get the first item
ResultSplit(0) = LeftB$(Str, lngResults(0) - 1)
' get the other items except the last one
For lngCount = 0 To lngCount - 2
ResultSplit(lngCount + 1) = MidB$(Str, lngResults(lngCount) + lngDelLen, lngResults(lngCount + 1) - lngResults(lngCount) - lngDelLen)
Next lngCount
' get the last item
ResultSplit(lngCount + 1) = RightB$(Str, lngExpLen - lngResults(lngCount) - lngDelLen + 1)
End If
Else
' clean any possible data that exists in the passed string array (like if it is multidimensional)
If Not Not ResultSplit Then Erase ResultSplit
' mysterious IDE error fix
Debug.Assert App.hInstance
' reset to one element, one dimension
ReDim ResultSplit(0 To 0)
' custom redimension: remove the items (this duplicates the VB6 Split behavior)
SafeArrayRedim Not Not ResultSplit, SafeArrayBound(0)
End If
End Sub
'TestObject 平均用时
'QuickSplit_Best 354.25
'QuickSplit 364.23
'QuickSplit2 365.31
'split() 3914.98
Public Sub QuickInput_File(File1 As String, Delimiter As String, ResultSplit() As String)
'最后的空行会忽略
Dim F As Long, UB As Long, I As Long
UB = 10001
F = FreeFile(0)
Open File1 For Input As #F
ReDim ResultSplit(10000)
'ReDim ResultSplit(114536)
Do Until EOF(F)
If I > UB Then UB = UB + 10000: ReDim Preserve ResultSplit(UB)
Line Input #F, ResultSplit(I)
I = I + 1
Loop
Close #F
If I > 0 Then ReDim Preserve ResultSplit(I - 1)
End Sub
Code:
class Program
{
static void Main(string[] args)
{
//定义文件路径
string path = @"D:\\code\\test.txt";
//创建 StreamReader 类的实例
StreamReader streamReader = new StreamReader(path);
//判断文件中是否有字符
while (streamReader.Peek() != -1)
{
//读取文件中的一行字符
string str = streamReader.ReadLine();
Console.WriteLine(str);
}
streamReader.Close();
}
}