Jump to content

Need help speeding up array loading.


Recommended Posts

Greetings,

Been awhile since I've been here, hope all is well.

I'm working on a script that will load a list of words into an array. It works fine, but it is rather slow. I'm looking for anyway to optimize it:

Func _LoadListToArray($sz_filename, ByRef $a_array,$i_wordsize)
    Local $i = 0
    Local $error = 0
    Local $tempword = ""
    Local $h_file = FileOpen($sz_filename,0)
    
    If ($h_file == -1) Then
        ;If FileOpen failed, it sets the handle to -1.
        $error = 1
    EndIf
    
    ;The FileReadLine() function will set @error to -1 if EOF is reached or 1 if there is some other error (e.g. No read permissions or missing file)
    While ($error == 0)
        $tempword = FileReadLine($h_file)
        $error = @error
        If ($error <> 0) Then
            ContinueLoop
        EndIf
        
        $i = $i + 1     ;Incrementer for line counting
        
        If (StringLen($tempword) <> $i_wordsize) Then
            If (MsgBox(4,"Error: " & $sz_filename,"Line #" & $i & " has more or less than " & $i_wordsize & " characters!" & @CRLF & "Continue loading?") == 7) Then    ;If they pressed "NO"
                $error = -1 ;We can set our own error code, but for simplicity, I'm going to use EOF error.
                ExitLoop
            EndIf       
        Else
            ;Word size is correct, let's add it to our array.
            _ArrayAdd($a_array, $tempword)
        EndIf
                
        $tempword = ""  ;Clear our temp variable for next word.
        Sleep(1)        ;Don't hog the CPU!!!
    WEnd
    
    ;Let's parse our error codes:
    Select
        Case $error == -1   ;EOF error
            MsgBox(0,"Completed loading file.", "Lines read in: " & $i)
        Case $error == 1    ;File read error
            MsgBox(0,"Error!", "An error occurred while trying to read from file: " & $sz_filename)
        Case $error == 0
            MsgBox(0,"Error!", "File read ended abruptly with no error code?!?")
        Case Else
            MsgBox(0,"Error!", "Unknown error code: " & $error)
    EndSelect
    
    FileClose($h_file)      ;Don't forget to close the file.
EndFunc

It takes a good 5 seconds to read in 101 lines and the other 2 files I have are in the range of 20,000 lines. Any clues on a better/faster way to approach this?

Thanx,

-CMR

Link to comment
Share on other sites

Hi,

did you try : _FileReadToArray ?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...