Late Binding

I l @ ve RuBoard

In Chapter 2, I discussed late binding and why it is a bad programming practice. I also mentioned that along with problems related to functional and coding standards, late binding is also a performance killer. This has everything to do with how late binding works. Essentially, late binding requires runtime function lookups to execute every method and evaluate every property, or to resolve type information. This adds significant overhead. To help convince you of this, I built a late binding performance sample called LateBindingPerformance (which is among this chapter's samples files). The following code is an excerpt, which should give you an idea of what we're testing.

 PubliclbClassAsObject PublicebClassAsEmployee PublicSubLateBoundTest() lbClass.Name= "Mr.Ted" lbClass.SSN= "123-45-6789" DimageAsInteger=Now.Subtract(lbClass.DateOfBirth).Days/365.25 EndSub PublicSubEarlyBoundTest() ebClass.Name= "Mr.Ted" ebClass.SSN= "123-45-6789" DimageAsInteger=_ CInt(Now.Subtract(ebClass.DateOfBirth).Days/365.25) EndSub PublicClassEmployee Privatem_nameAsString Privatem_ssnAsString Privatem_dobAsDate PublicSubNew(ByValnameAsString,ByValssnAsString,_ ByValdobAsDate) MyBase.New() m_name=name m_ssn=ssn m_dob=dob EndSub PublicPropertyName()AsString Get Returnm_name EndGet Set(ByValValueAsString) m_name=Value EndSet EndProperty PublicPropertySSN()AsString Get Returnm_ssn EndGet Set(ByValValueAsString) m_ssn=Value EndSet EndProperty PublicPropertyDateOfBirth()AsDate Get Returnm_dob EndGet Set(ByValValueAsDate) m_dob=Value EndSet EndProperty EndClass 

What we're doing in the LateBoundTest and EarlyBoundTest methods is admittedly arbitrary, but it serves its purpose ”accessing both early and late-bound methods and properties. The results demonstrate quite a difference. Check out the program output:

 EarlyBoundTime:1001420 LateBoundTime:312342898 EarlyBindingexampleis~312timesfaster 

It's important to understand that this does not tell the whole story. In the LateBindingPerformance sample, the properties we access do very little work. As a result, the overhead of calling a late-bound property or method is large compared to the execution time of that property or method. If the property or method were to do a lot of work, the amount of time added by the overhead of late binding would be minimal, if not insignificant, by comparison. Imagine that you call a method that takes a half-second to execute. The cost of calling a late-bound method is probably measured in hundreds of processor cycles. So making a late-bound call in this case wouldn't affect your performance in any noticeable way. (But remember that there are other reasons to avoid late binding. You should just consider it a generally bad idea.)

I l @ ve RuBoard


Designing Enterprise Applications with Microsoft Visual Basic .NET
Designing Enterprise Applications with Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 073561721X
EAN: 2147483647
Year: 2002
Pages: 103

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net