Forum Discussion

ChrisPro's avatar
ChrisPro
Contributor
11 years ago

Read 50 variables in IN file is slow

In order to read 50 variables in INI file, I use this following code 50 times:

    var lIni = Storages.INI(...);

    var lSection = lIni.GetSubSection(...);

    lSection.GetOption(..., "");

 

It is verry slow : 30 seconds is necessary.

 

An idea to accelerate the treatment?

 

Thanks.

 

  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 3 rankChampion Level 3

    Hi,

     

    My guess is that the major time penalty is because of this line

        var lIni = Storages.INI(...);

    been executed 50 times. I think so because this line results in file open OS operation which may be resource-consuming (for example, if the file is large and is located on the network resource).

      • joseph_michaud's avatar
        joseph_michaud
        Moderator

        I tried a simple test reading 50 options entries in a section all at once, and then again where I opened the INI file, searched for the section/option 50 separate times.

         

        Both tests took less than second.

         

        Perhaps you could post your code and attach the INI file so that we could take a look.

         

         

  • ''' <Summary>ParseIniFileSections</Summary>
    ''' <Param Type="String">[ByVal] strFilePath</Param>
    ''' <Returns Type="Object">Scripting.Dictionary</Returns>
    ''' <Remarks>Parse Ini file and add string values for each 
    '''  attribute parsed from each sub section of the specified 
    ''' INI file to the dictionary object</Remarks>
    Function ParseIniFileSections(strFilePath)
    
       Dim dict : Set dict = Nothing
       Dim i, index, iniFile, subset, value, valueName
        
       If aqFileSystem.Exists(strFilePath) Then
          Set iniFile = Storages.INI(strFilePath)
          Set dict = CreateObject("Scripting.Dictionary")
          
          With iniFile
             ' parse each section of specified ini file
             For i = 0 To .SectionCount - 1
                Set subset = .GetSubSectionByIndex(i)
                
                ' Parse/Read sub section attributes/values
                For index = 0 To subset.OptionCount - 1
                   valueName = subset.GetOptionName(index)
                   value = subset.GetOption(valueName, "")
                   Call dict.add(valueName, value)
                Next
             Next
          End With
       Else
          Log.Warning aqString.Format("[%s] - Path does not exist!")   
       End If
    
       Set ParseIniFileSections = dict   
    
    End Function   ' ParseIniFileSections

     

    An alternative is to use VBScript&colon;

    ''' <summary>ReadIniFile</summary>
    ''' <params type="String">ByVal sFSpec</params>
    ''' <returns type="String">Scripting.Dictionary</returns>
    Function ReadIniFile(sFSpec)
       
       Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
       Dim dicTmp : Set dicTmp = CreateObject("Scripting.Dictionary")
       Dim tsIn   : Set tsIn   = fso.OpenTextFile(sFSpec, ForReading)
       Dim sLine, sSec, aKV
       
       Do Until tsIn.AtEndOfStream
          sLine = Trim(tsIn.ReadLine())
          
          If Left(sLine, 1) = "[" Then
             sSec = Trim(Mid(sLine, 2, Len(sLine) - 2))
             Set dicTmp(sSEc) = CreateObject("Scripting.Dictionary")
          Else
             'If "" <> sLine Then
             If StrComp(sLine, "", 1) <> 0 Then
                aKV = Split(sLine, "=")
                
                If UBound(aKV) = 1 Then
                   dicTmp(sSec)(Trim(aKV(0))) = Trim(aKV(1))
                End If
             End If
          End If
       Loop
       
       tsIn.Close
       
       Set ReadIniFile = dicTmp
       Set fso = Nothing
    
    End Function    ' ReadIniFile