dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17

Thread: Option Strict Faster?

  1. #16
    Bill McCarthy Guest

    Re: Option Strict Faster?

    Hi Larry,

    Just a couple of points to clarify. The code in the Form_Load procedure should
    be the same in both cases. The reason for this is simple. A constant of value 1
    is being assigned to a boolean. This is evaluated at compile time to a simple
    assignment for the boolean of True, as 1 <> 0.

    In the second snippet of code, the Form1_Click procedure, there are different
    operations being performed. If you think logically about what the compiler must
    do in the two cases, the difference should be apparent.

    For the strict off code, Text = sender.Text, the compiler has to get the type
    information for the object at runtime and attempt to access a "Text" member of
    the object. It has no information as to what the "Text" member is, a field, a
    method or property. It also has no information as to the return type of the
    object's "Text" member.

    For the strict code, Text = Ctype(sender,Form).Text, then the compiler jsut
    simply has to issue a cast to see if the sender is of the type Form. This get's
    resolved at runtime. However, if it is, then the Text property can be resolved
    to a call to the Form type's Text property get method. As this method is a
    virtual method, there is still some more runtime resolving going on here. The
    assignemnt to a string however can be validated at compile time based on the
    Form's type information.

    So both the option strict on and option strict off code in this exampel do have
    late bound calls going on. Jsut in the option strict off case it has all the
    late bound calls the strict on code has to have, but it alsoi has a lot, one
    **** of a lot more.


    "Larry Serflaten" <serflaten@usinternet.com> wrote in message
    news:3c1785f6@147.208.176.211...
    >
    > "Cali LaFollett" <cali@no-spam-please-visionized.com> wrote
    > >
    > > Have you tried something similar with your own classes instead of integers?
    > > Integers may be a little different since, AFAIK, the whole type casting
    > > thing is to ensure that there isn't an overflow and you are aware of what
    > > you are doing. A custom class where you aren't doing the type casting may be
    > > a little different. I am curious to see what the IL would look like in that
    > > case.

    >
    > Perhaps this will help fill in a few gaps....
    >
    > I used two cases that trip up when Option Strict is on;
    >
    > Private Sub Form1_Load(...) Handles MyBase.Load
    > Dim a As Boolean
    > 'a = 1
    > a = CBool(1)
    > End Sub
    >
    > Private Sub Form1_Click(ByVal sender As Object, ...) Handles MyBase.Click
    > Dim Text As String
    > 'Text = sender.Text
    > Text = CType(sender, Form).Text
    > End Sub
    >
    >
    > In Form_Load there was no difference between using either expression:
    >
    > Private Sub Form1_Load(...) Handles MyBase.Load
    > 00000000 push ebp
    > 00000001 mov ebp,esp
    > 00000003 sub esp,0Ch
    > 00000006 mov dword ptr [ebp-4],ecx
    > 00000009 mov dword ptr [ebp-8],edx
    > 0000000c xor eax,eax
    > 0000000e nop
    > a = CBool(1)
    > 0000000f mov eax,1
    > End Sub
    >
    > The above shows line 0x000f as the one ML instruction used for the
    > assignment command. It is the same line used when a = 1 is disassembled.
    >
    > In Form_Click there was a difference:
    >
    > Strict Off
    >
    > Private Sub Form1_Click(...) Handles MyBase.Click
    > 00000000 push ebp
    > 00000001 mov ebp,esp
    > 00000003 sub esp,0Ch
    > 00000006 push edi
    > 00000007 push esi
    > 00000008 mov dword ptr [ebp-4],ecx
    > 0000000b mov esi,edx
    > 0000000d xor edi,edi
    > 0000000f nop
    > Text = sender.Text
    > 00000010 push dword ptr ds:[01B103FCh]
    > 00000016 xor edx,edx
    > 00000018 mov ecx,2CB6B2Eh
    > 0000001d call FD1FB934
    > 00000022 push eax
    > 00000023 push 0
    > 00000025 mov ecx,esi
    > 00000027 xor edx,edx
    > 00000029 call dword ptr ds:[035B6F44h]
    > 0000002f mov ecx,eax
    > 00000031 call dword ptr ds:[035B7170h]
    > 00000037 mov edi,eax
    > End Sub
    >
    >
    > Strict On
    >
    > Private Sub Form1_Click(...) Handles MyBase.Click
    > 00000000 push ebp
    > 00000001 mov ebp,esp
    > 00000003 sub esp,0Ch
    > 00000006 push edi
    > 00000007 push esi
    > 00000008 mov dword ptr [ebp-4],ecx
    > 0000000b mov esi,edx
    > 0000000d xor edi,edi
    > 0000000f nop
    > Text = CType(sender, Form).Text
    > 00000010 mov edx,esi
    > 00000012 mov ecx,2E84518h
    > 00000017 call 5D4D92CE
    > 0000001c mov ecx,eax
    > 0000001e mov eax,dword ptr [ecx]
    > 00000020 call dword ptr [eax+000000D8h]
    > 00000026 mov edi,eax
    > End Sub
    >
    > To keep those EULA lawyers from crowding in, this is not a benchmark

    comparison.
    > It is validating evidence that in this case, using Option Strict helps to

    remove VB.Net's
    > own compiler conversion routines.
    >
    > Maybe not so surprizingly, the difference amounts to one extra call, and a

    handfull of
    > additional instructions, if you opt to run with Strict Off. My opinion would

    be that it
    > really wouldn't be a speed performance issue anywhere other than in some forms
    > of critical code.
    >
    > YMMV
    > LFS
    >
    >
    >
    >
    >
    >
    >




  2. #17
    Larry Serflaten Guest

    Re: Option Strict Faster?


    "Bill McCarthy" <Bill_McC@iprimus.com.au> wrote
    >
    > In the second snippet of code, the Form1_Click procedure, there are different
    > operations being performed. If you think logically about what the compiler must
    > do in the two cases, the difference should be apparent.


    Yes, even at the ML level, just one simple little call can bring a lot more code into
    the picture. But, as in this case, there is a little bit of room when responding to
    user events. Thanks for the clarification though, I'm glad you offered a bit of your
    own expertise. If you want to say there is a speed concern, I'll just shut up, and
    see which style I'm more comfortable with....

    FWIW, I did leave myself a little wiggle room:

    > > YMMV


    <g>
    LFS



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center