Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any Performance Advantage Using With.. End With? 3

Status
Not open for further replies.

jv6886

Programmer
Dec 31, 2002
46
0
0
US
Aside from the more obvious advantages of saving typing and easier reading, does anybody know if there are any performance advantages to using the With... End With statements?
 
Hi,

The answare is short:

NO!

It's only to save typing. But like every "cosmetics" it's slower.

Think! How can be faster???

Tibi

 
1127, It is faster because VB doesn't have to dereference the object (it spends time on each '.') It is vaguely (and only vaguely) equivalent to putting variable assignments outside loops.
 
Wouldn't it be the same thing as just typing it out after it's compiled? -Greg :-Q

flaga.gif
 
No its not the same after it is compiled-

If you have a loop where you reference a control or object over and over, use the With/End With statement block. It will stop VB from constantly looking up the address reference to the object each time it is encountered. VB will look up the reference only once at the beginning of the block saving redundant lookups.

You will increase performance in the right situations.
 
Let me clarify quickly--

strongm is right - it is always a 'bit' faster.

I think loops are the only place where it will be of much help in regards to performance.

1127 you have been misinformed.

 
Strongm is correct - it increases runtime performance because the interpreter doesn't have to check for a valid object reference every time it encounters the "."

Obviously, doing it for a single reference is not going to get you any performance benefits:
Code:
With MyCat
   .Color = vbColorBlue
End With
But if you had a lot of member variables to set you'd see some gain:
Code:
With MyCat
   .Color = vbColorBlue
   .Weight = 10
   .Height = 35
   .FurLength = FurLengthEnum.Short
   .HasBell = True
   .HasFurball = False
   Set .Picture = MyImage
End With
Or if you were in a loop, you'd see the biggest gains:
Code:
For i = 0 To Litter.Count - 1
   Set oKitten = New Kitten
   With oKitten
      .Color = vbColorBlue
      .Weight = 10
      .Height = 35
      .FurLength = FurLengthEnum.Short
      .HasBell = True
      .HasFurball = False
      Set .Picture = MyImage[i]
   End With
   Cats.Add oKitten
   Set oKitten = Nothing
Next i

Hope this helps.
Chip H.
 
The performance gain you get from using WITH is marked by 2 thing
1) how many levels down are you going in the object tree
2) how many times you are using methods and properties in that level

Normally in a situation like this

Label1.Caption = "hello"
Label1.Top = 0
Label1.Left = 0

VB get the address of the Label1 object
then gets the address of Caption property
Set the caption
then gets the address of the Label1 object
then gets the address of Top property
Set the Top
then gets the address of the Label1 object
then gets the address of Left property
Set Left

when using the following syntax

With Label1
.Caption = "Hello"
.Top = 0
.Left = 0
End With

VB get the address of the Label1 object
then gets the address of Caption property
Set the caption
then gets the address of Top property
Set the Top
then gets the address of Left property
Set Left

Thus you save 2 internal calls

To give you an idea of performance

Code:
    With Label1
        For i = 1 To 10000000
            .Caption = "Hello"
            .Caption = "Hello"
            .Caption = "Hello"
            .Caption = "Hello"
            .Caption = "Hello"
            .Caption = "Hello"
            .Caption = "Hello"
            .Caption = "Hello"
            .Caption = "Hello"
            .Caption = "Hello"
        Next
    End With
is ~20-25% faster than the code below. The majority of the time being on the assignment.
Code:
    For i = 1 To 10000000
        Label1.Caption = "Hello"
        Label1.Caption = "Hello"
        Label1.Caption = "Hello"
        Label1.Caption = "Hello"
        Label1.Caption = "Hello"
        Label1.Caption = "Hello"
        Label1.Caption = "Hello"
        Label1.Caption = "Hello"
        Label1.Caption = "Hello"
        Label1.Caption = "Hello"
    Next

This performance would get larger % wise if you went down more than 1 level and/or used more method/properties of that level.

To give you an idea run the code above
on the P600 here it ran in 108 seconds aposed to 138
 
I too am siding with strongm, because he's 100% correct.

It is good coding practice to use with...end with, and don't let anyone tell you otherwise.

Sure, most of the time, or at least a good deal of the time, it's not going to be humanly noticable if you do it without with...end with, but get in the habbit of it, and pretty soon you'll be glad you did.

Tuna - It's fat free until you add the Mayo!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top