What is the fastest way to Parse a line in Delphi?

前端 未结 9 1961
深忆病人
深忆病人 2020-12-13 01:18

I have a huge file that I must parse line by line. Speed is of the essence.

Example of a line:

Token-1   Here-is-the-Next-Token      La         


        
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 01:55

    If speed is of the essence, custom code is the answer. Check out the Windows API that will map your file into memory. You can then just use a pointer to the next character to do your tokens, marching through as required.

    This is my code for doing a mapping:

    procedure TMyReader.InitialiseMapping(szFilename : string);
    var
    //  nError : DWORD;
        bGood : boolean;
    begin
        bGood := False;
        m_hFile := CreateFile(PChar(szFilename), GENERIC_READ, 0, nil, OPEN_EXISTING, 0, 0);
        if m_hFile <> INVALID_HANDLE_VALUE then
        begin
            m_hMap := CreateFileMapping(m_hFile, nil, PAGE_READONLY, 0, 0, nil);
            if m_hMap <> 0 then
            begin
                m_pMemory := MapViewOfFile(m_hMap, FILE_MAP_READ, 0, 0, 0);
                if m_pMemory <> nil then
                begin
                    htlArray := Pointer(Integer(m_pMemory) + m_dwDataPosition);
                    bGood := True;
                end
                else
                begin
    //              nError := GetLastError;
                end;
            end;
        end;
        if not bGood then
            raise Exception.Create('Unable to map token file into memory');
    end;
    

提交回复
热议问题