Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using With
#1
My teach told me in VB, With's inside of With's are slow. When I added With's to the LoadPlayer sub, my Log In is instantaneous:

From:
Code:
Sub LoadPlayer(ByVal index As Long, ByVal Name As String)
    Dim FileName As String
    Dim i As Long
    Dim n As Long
    Dim nFileNum As Integer
    
    FileName = App.Path & "\Accounts\" & Trim(Name) & ".bin"

    nFileNum = FreeFile
    Open FileName For Binary As #nFileNum
    
    Get #nFileNum, , Player(index).Login
    Get #nFileNum, , Player(index).Password
    For i = 1 To MAX_CHARS
        'General Information
        Get #nFileNum, , Player(index).Char(i).Name
        Get #nFileNum, , Player(index).Char(i).Class
        Get #nFileNum, , Player(index).Char(i).Sex
        Get #nFileNum, , Player(index).Char(i).Sprite
        Get #nFileNum, , Player(index).Char(i).Level
        Get #nFileNum, , Player(index).Char(i).Exp
        Get #nFileNum, , Player(index).Char(i).Access
        Get #nFileNum, , Player(index).Char(i).PK
        Get #nFileNum, , Player(index).Char(i).Guild
        
        'Vitals
        Get #nFileNum, , Player(index).Char(i).HP
        Get #nFileNum, , Player(index).Char(i).MP
        Get #nFileNum, , Player(index).Char(i).SP
        
        'Stats
        Get #nFileNum, , Player(index).Char(i).STR
        Get #nFileNum, , Player(index).Char(i).DEF
        Get #nFileNum, , Player(index).Char(i).SPEED
        Get #nFileNum, , Player(index).Char(i).MAGI
        Get #nFileNum, , Player(index).Char(i).POINTS
        
        'Worn Equipment
        Get #nFileNum, , Player(index).Char(i).ArmorSlot
        Get #nFileNum, , Player(index).Char(i).WeaponSlot
        Get #nFileNum, , Player(index).Char(i).HelmetSlot
        Get #nFileNum, , Player(index).Char(i).ShieldSlot

        ' Check to make sure that they aren't on map 0, if so reset'm
        If Player(index).Char(i).Map = 0 Then
            Player(index).Char(i).Map = START_MAP
            Player(index).Char(i).x = START_X
            Player(index).Char(i).y = START_Y
        End If
        
        ' Position
        Get #nFileNum, , Player(index).Char(i).Map
        Get #nFileNum, , Player(index).Char(i).x
        Get #nFileNum, , Player(index).Char(i).y
        Get #nFileNum, , Player(index).Char(i).Dir
        
        ' Inventory
        For n = 1 To MAX_INV
            Get #nFileNum, , Player(index).Char(i).Inv(n).Num
            Get #nFileNum, , Player(index).Char(i).Inv(n).Value
            Get #nFileNum, , Player(index).Char(i).Inv(n).Dur
        Next n
        
        ' Spells
        For n = 1 To MAX_PLAYER_SPELLS
            Get #nFileNum, , Player(index).Char(i).Spell(n)
        Next n
    Next i
    Close #nFileNum
End Sub
To:
Code:
Sub LoadPlayer(ByVal index As Long, ByVal Name As String)
    Dim FileName As String
    Dim i As Long
    Dim n As Long
    Dim nFileNum As Integer
    
    FileName = App.Path & "\Accounts\" & Trim(Name) & ".bin"

    nFileNum = FreeFile
    Open FileName For Binary As #nFileNum
    
    With Player(index)
        Get #nFileNum, , .Login
        Get #nFileNum, , .Password
        For i = 1 To MAX_CHARS
            With .Char(i)
                'General Information
                Get #nFileNum, , .Name
                Get #nFileNum, , .Class
                Get #nFileNum, , .Sex
                Get #nFileNum, , .Sprite
                Get #nFileNum, , .Level
                Get #nFileNum, , .Exp
                Get #nFileNum, , .Access
                Get #nFileNum, , .PK
                Get #nFileNum, , .Guild
                
                'Vitals
                Get #nFileNum, , .HP
                Get #nFileNum, , .MP
                Get #nFileNum, , .SP
                
                'Stats
                Get #nFileNum, , .STR
                Get #nFileNum, , .DEF
                Get #nFileNum, , .SPEED
                Get #nFileNum, , .MAGI
                Get #nFileNum, , .POINTS
                
                'Worn Equipment
                Get #nFileNum, , .ArmorSlot
                Get #nFileNum, , .WeaponSlot
                Get #nFileNum, , .HelmetSlot
                Get #nFileNum, , .ShieldSlot
        
                ' Check to make sure that they aren't on map 0, if so reset'm
                If .Map = 0 Then
                    .Map = START_MAP
                    .x = START_X
                    .y = START_Y
                End If
                
                ' Position
                Get #nFileNum, , .Map
                Get #nFileNum, , .x
                Get #nFileNum, , .y
                Get #nFileNum, , .Dir
                
                ' Inventory
                For n = 1 To MAX_INV
                    With .Inv(n)
                        Get #nFileNum, , .Num
                        Get #nFileNum, , .Value
                        Get #nFileNum, , .Dur
                    End With
                Next n
                
                ' Spells
                For n = 1 To MAX_PLAYER_SPELLS
                    Get #nFileNum, , .Spell(n)
                Next n
            End With
        Next i
    End With
    
    Close #nFileNum
End Sub
So I'm lead to believe using With's are a huge optimization, anyone think differently or am I right?
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)