vbGore Free Online RPG Engine

Revolutionizing Visual Basic ORPG Development
It is currently Wed Jun 19, 2013 1:06 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Avoid with blocks (especially in loops)
PostPosted: Mon Apr 02, 2007 1:14 am 
+3 Gloves of Agility

Joined: Fri Mar 23, 2007 1:15 pm
Posts: 164
Note on with blocks: don't use them a lot, they slow down stuff if used a lot (or so i've heard)

Well , at least in loops

see for youself:
Code:
Option Explicit

Private Declare Function GetTickCount Lib "kernel32" () As Long


Private Type somestruct
    x As Integer
    y As Integer
    var1 As Integer
    var2 As Integer
    var3 As Integer
    str As String
    somemore As Byte
    isit As Boolean
End Type


Private Sub Form_Load()
    Dim t1 As somestruct
    Dim t2 As somestruct
   
    Dim i As Long
   
    Dim tick As Long
   
    tick = GetTickCount
    For i = 0 To 1000000
        With t1
            .x = 12345
            .y = 13424
            .var1 = 12342
            .var2 = 31341
            .var3 = 24342
            .str = "thisisatest"
            .somemore = 125
            .isit = True
        End With
    Next
    Debug.Print "t1 " & GetTickCount - tick
   
   
    tick = GetTickCount
    For i = 0 To 1000000
            t2.x = 12345
            t2.y = 13424
            t2.var1 = 12342
            t2.var2 = 31341
            t2.var3 = 24342
            t2.str = "thisisatest"
            t2.somemore = 125
            t2.isit = True
    Next
    Debug.Print "t2 " & GetTickCount - tick
   
End Sub


Result:
t1 500ms
t2 375ms


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 6:34 am 
Site Admin

Joined: Sun Jul 30, 2006 4:00 pm
Posts: 1230
Very odd, we have heard it the other way around and when I run the example I get

t1 204
t2 218
t1 203
t2 218
.....

I'm gonna double up the amount of work real quick....

t1 422
t2 453

I see conflicting information while searching. Can a few other people test the example on their machine?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 11:15 am 
Site Admin

Joined: Fri Jul 14, 2006 4:00 pm
Posts: 11230
Location: Washington
Thanks for the post, Drake. Unfortunately, I have to disagree. First off, your testing enviornment is quite inefficient. Try checking this:

http://www.vbgore.com/Testing_VB_code_speed

Its pretty much the same idea, but performs multiple tests in each "set" instead of just one, along with DoEvents between sets are vital since it helps prevent other threads from being executed during the loops. Finally, you're running in a normal priority environment, which means a lot of processing is still going on in the background. May not be much in relative to what it can be, but it does fluctuate the results. Finally, you should be testing on the same variable when possible - often this won't be any problem at all, but its just good practice since with larger variables, it could change things.

Now for what with blocks do exactly, I am not positive (I used to know better before), but I believe it is along the lines of they assign a pointer in memory to where the "With"ed object is. This means that for an example like yours, it is more then likely going to be slower, and that is very true and also not an uncommon time for With Blocks to be used. With blocks, when in situations of heavily structured objects with multiple references, will come in handy. They also are good when you have to perform calculations to get to that object. Often its easy to stay away from such huge blocks, but it does happen.

Finally, as for my results of your test, I got that T1 is slower, too, but by a very insignificant amount (around 1% slower). :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 1:32 am 
+3 Gloves of Agility

Joined: Fri Mar 23, 2007 1:15 pm
Posts: 164
ah darn :p


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group