Performance gain? 
Author Message
 Performance gain?

Hi.

When using VB6 we are encouraged to use the With statement in order to avoid
unwanted memory load. In VB.NET the With statement also has any benefits?

Thanks,

Antonio Maciel



Tue, 13 Dec 2005 05:50:44 GMT  
 Performance gain?


Quote:
> Hi.

> When using VB6 we are encouraged to use the With statement in order to
avoid
> unwanted memory load. In VB.NET the With statement also has any benefits?

> Thanks,

> Antonio Maciel

I think it makes the sourcecode also more readable. when you use the with
statement.

G&R



Tue, 13 Dec 2005 06:06:56 GMT  
 Performance gain?
Hello,


Quote:
> When using VB6 we are encouraged to use the With statement
> in order to avoid unwanted memory load. In VB.NET the With
> statement also has any benefits?

Better performance.

Bad:

\\\
Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Peter")
Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Frank")
Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Harry")
Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Fred")
Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Bill")
///

Better:

\\\
With Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo")
    .Bar("Peter")
    .Bar("Frank")
    .Bar("Harry")
    .Bar("Fred")
    .Bar("Bill")
End With
///

Regards,
Herfried K. Wagner
--
MVP VB Classic, VB .NET
http://www.mvps.org/dotnet



Tue, 13 Dec 2005 06:51:18 GMT  
 Performance gain?
In what way is this bad?  The only thing the With does is allow a way to not
have to type in the full path of the object in question.  When you look at
the IL code generated, it's as if you typed in the complete object path.  In
other words, the object after the With statement gets prepended to the
objects in the with block (exactly as if you had typed in in yourself
without the with statement).  So in all actuality, there is no "performance"
other than "convenience" in the editor.  At runtime, it's exactly the same.

Thanks,
Shawn



Quote:
> Hello,


> > When using VB6 we are encouraged to use the With statement
> > in order to avoid unwanted memory load. In VB.NET the With
> > statement also has any benefits?

> Better performance.

> Bad:

> \\\
> Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Peter")
> Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Frank")
> Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Harry")
> Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Fred")
> Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Bill")
> ///

> Better:

> \\\
> With Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo")
>     .Bar("Peter")
>     .Bar("Frank")
>     .Bar("Harry")
>     .Bar("Fred")
>     .Bar("Bill")
> End With
> ///

> Regards,
> Herfried K. Wagner
> --
> MVP VB Classic, VB .NET
> http://www.mvps.org/dotnet



Tue, 13 Dec 2005 23:41:32 GMT  
 Performance gain?
in VB6 there was surely a difference, in .NET, i dont know for sure.


Quote:
> In what way is this bad?  The only thing the With does is allow a way to
not
> have to type in the full path of the object in question.  When you look at
> the IL code generated, it's as if you typed in the complete object path.
In
> other words, the object after the With statement gets prepended to the
> objects in the with block (exactly as if you had typed in in yourself
> without the with statement).  So in all actuality, there is no
"performance"
> other than "convenience" in the editor.  At runtime, it's exactly the
same.

> Thanks,
> Shawn



> > Hello,


> > > When using VB6 we are encouraged to use the With statement
> > > in order to avoid unwanted memory load. In VB.NET the With
> > > statement also has any benefits?

> > Better performance.

> > Bad:

> > \\\
> > Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Peter")
> > Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Frank")
> > Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Harry")
> > Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Fred")
> > Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo").Bar("Bill")
> > ///

> > Better:

> > \\\
> > With Foo.Bar.Baz(10).Bas(33).Gac(99).Bar("foo")
> >     .Bar("Peter")
> >     .Bar("Frank")
> >     .Bar("Harry")
> >     .Bar("Fred")
> >     .Bar("Bill")
> > End With
> > ///

> > Regards,
> > Herfried K. Wagner
> > --
> > MVP VB Classic, VB .NET
> > http://www.mvps.org/dotnet



Tue, 13 Dec 2005 23:53:47 GMT  
 Performance gain?
Hello,


Quote:
> In what way is this bad?  The only thing the With does is
> allow a way to not have to type in the full path of the object
> in question.

The full path can contain property/method calls.

Quote:
> When you look at the IL code generated, it's as if you typed
> in the complete object path.

But only once.

Quote:
> In other words, the object after the With statement gets
> prepended to the objects in the with block (exactly as if you
> had typed in in yourself without the with statement).

No. Not always. See the sample below.

Quote:
> So in all actuality, there is no "performance"
> other than "convenience" in the editor.  At runtime, it's
> exactly the same.

No.

Have a look at this code and run it.

\\\
Private Sub Test()
    Dim f As New Foo
    MsgBox("1")
    With f.Bar(0)
        .SayHello()
        .SayHello()
    End With
    MsgBox("2")
    f.Bar(0).SayHello()
    f.Bar(0).SayHello()
    MsgBox("end")
End Sub
.
.
.
Public Class Foo
    Private m_FooBars As ArrayList

    Public Sub New()
        m_FooBars = New ArrayList
        m_FooBars.Add(New FooBar)
    End Sub

    Public Property Bar(ByVal Index As Integer) As FooBar
        Get
            MsgBox("Bar " & Index.ToString())    ' <--
            Return m_FooBars(Index)
        End Get
        Set(ByVal Value As FooBar)
            m_FooBars(Index) = Value
        End Set
    End Property
End Class

Public Class FooBar
    Public Sub SayHello()
        MsgBox("Hello")
    End Sub
End Class
///

Regards,
Herfried K. Wagner
--
MVP VB Classic, VB .NET
http://www.mvps.org/dotnet



Wed, 14 Dec 2005 00:06:51 GMT  
 Performance gain?
In the IL in this case it's not immediately obvious which is the better
route because the IL generated in the non-with statement has fewer lines of
IL and the one with the With stores a pointer the others use, which really
appeared to be the same as the non-with in the end... but not until I viewed
the dissassembly (of course, it's not the true optimized assembly that would
be output in a release mode compile) shows the real difference that the
non-with is less efficient (I'm an assembly language programmer so I can see
the difference).

You can see the second version is redundant in its maintenance and cleanup
where as the one with the With statement uses the same maintenance, prolog,
and epilogue code only once, and then the calls are the only redundancy and
that's what would be expected.

I'll delve deeper into more "real" world scenarios (we have them all over
the place in our 100k+ lines of code project here) and see how they all pan
out...

------------------------------------
    61:    Private Sub Test()
00000000  push        ebp
00000001  mov         ebp,esp
00000003  sub         esp,0Ch
00000006  push        edi
00000007  push        esi
00000008  push        ebx
00000009  mov         dword ptr [ebp-4],ecx
0000000c  xor         esi,esi
0000000e  xor         edi,edi
00000010  nop
    62:       Dim f As New Foo()
00000011  mov         ecx,314D794h
00000016  call        FD763588
0000001b  mov         ebx,eax
0000001d  mov         ecx,ebx
0000001f  call        dword ptr ds:[0314D7CCh]
00000025  mov         esi,ebx
    64:       MsgBox("1")
00000027  push        0
00000029  mov         ecx,dword ptr ds:[020501B0h]
0000002f  xor         edx,edx
00000031  call        dword ptr ds:[0314DA24h]
00000037  nop
    66:       With f.Bar(0)
00000038  mov         ecx,esi
0000003a  xor         edx,edx
0000003c  cmp         dword ptr [ecx],ecx
0000003e  call        dword ptr ds:[0314D7D0h]
00000044  mov         ebx,eax
00000046  mov         edi,ebx
    67:          .SayHello()
00000048  mov         ecx,edi
0000004a  cmp         dword ptr [ecx],ecx
0000004c  call        dword ptr ds:[0314DD4Ch]
    67:          .SayHello()
00000052  nop
    68:          .SayHello()
00000053  mov         ecx,edi
00000055  cmp         dword ptr [ecx],ecx
00000057  call        dword ptr ds:[0314DD4Ch]
0000005d  nop
    69:       End With
0000005e  xor         edi,edi
    71:       MsgBox("2")
00000060  push        0
00000062  mov         ecx,dword ptr ds:[02050634h]
00000068  xor         edx,edx
0000006a  call        dword ptr ds:[0314DA24h]
00000070  nop
    73:       f.Bar(0).SayHello()
00000071  mov         ecx,esi
00000073  xor         edx,edx
00000075  cmp         dword ptr [ecx],ecx
00000077  call        dword ptr ds:[0314D7D0h]
0000007d  mov         ebx,eax
0000007f  mov         ecx,ebx
00000081  cmp         dword ptr [ecx],ecx
00000083  call        dword ptr ds:[0314DD4Ch]
00000089  nop
    74:       f.Bar(0).SayHello()
0000008a  mov         ecx,esi
0000008c  xor         edx,edx
0000008e  cmp         dword ptr [ecx],ecx
00000090  call        dword ptr ds:[0314D7D0h]
00000096  mov         ebx,eax
00000098  mov         ecx,ebx
0000009a  cmp         dword ptr [ecx],ecx
0000009c  call        dword ptr ds:[0314DD4Ch]
000000a2  nop
    76:       MsgBox("end")
000000a3  push        0
    76:       MsgBox("end")
000000a5  mov         ecx,dword ptr ds:[02050638h]
000000ab  xor         edx,edx
000000ad  call        dword ptr ds:[0314DA24h]
000000b3  nop
    77:    End Sub
000000b4  nop
000000b5  pop         ebx
000000b6  pop         esi
000000b7  pop         edi
000000b8  mov         esp,ebp
000000ba  pop         ebp
000000bb  ret

------------------------------------



Quote:
> Hello,


> > In what way is this bad?  The only thing the With does is
> > allow a way to not have to type in the full path of the object
> > in question.

> The full path can contain property/method calls.

> > When you look at the IL code generated, it's as if you typed
> > in the complete object path.

> But only once.

> > In other words, the object after the With statement gets
> > prepended to the objects in the with block (exactly as if you
> > had typed in in yourself without the with statement).

> No. Not always. See the sample below.

> > So in all actuality, there is no "performance"
> > other than "convenience" in the editor.  At runtime, it's
> > exactly the same.

> No.

> Have a look at this code and run it.

> \\\
> Private Sub Test()
>     Dim f As New Foo
>     MsgBox("1")
>     With f.Bar(0)
>         .SayHello()
>         .SayHello()
>     End With
>     MsgBox("2")
>     f.Bar(0).SayHello()
>     f.Bar(0).SayHello()
>     MsgBox("end")
> End Sub
> .
> .
> .
> Public Class Foo
>     Private m_FooBars As ArrayList

>     Public Sub New()
>         m_FooBars = New ArrayList
>         m_FooBars.Add(New FooBar)
>     End Sub

>     Public Property Bar(ByVal Index As Integer) As FooBar
>         Get
>             MsgBox("Bar " & Index.ToString())    ' <--
>             Return m_FooBars(Index)
>         End Get
>         Set(ByVal Value As FooBar)
>             m_FooBars(Index) = Value
>         End Set
>     End Property
> End Class

> Public Class FooBar
>     Public Sub SayHello()
>         MsgBox("Hello")
>     End Sub
> End Class
> ///

> Regards,
> Herfried K. Wagner
> --
> MVP VB Classic, VB .NET
> http://www.mvps.org/dotnet



Wed, 14 Dec 2005 01:34:17 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Structure vs Class - Performance Gain?

2. Are there performance gains from normalizing data?

3. 2.5 to 2.6SP1 performance gain

4. What is the gain to use VB dll in COM+ performance wise?

5. ADO's Performance vs DAO's Performance

6. why cant i gain access to Outlook?

7. Gaining access to property tab

8. gain VBA access to the Compatibility Font Substitution

9. Gaining control over IE 5 Printing

10. Icons - How to gain access to the Mask, image as well as the Color-table : ExtractIcon

11. How to gain access to API functions from VB6

12. Icons - How to gain access to the Mask, image as well as the Color-table : ExtractIcon

 

 
Powered by phpBB® Forum Software