Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1460

vb6 Fast ReadLine,QuickSplit(Like streamReader.ReadLine)

$
0
0
quick split is 132% faster than line input

If you use pointers, you don't need to have MidB$ for each line of string, will it be faster?


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
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();
    }
}


Viewing all articles
Browse latest Browse all 1460

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>