Atlanta Custom Software Development 

   Search        Code/Page

User Login



Forgot the Password?
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
» Regular Expr Tester
» Free Tools

Friend Procedures are faster than Public ones

Total Hit ( 2340)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article



It might surprise you to learn that Friend procedures are sensibly faster than Public ones. You can prove this yourself by creating an ActiveX EXE project with one Private class and one Public class (Instancing = MultiUse), and then add the following code in both class modules:

Click here to copy the following block
Public Sub PublicSub(ByVal value As Long)
End Sub

Public Function PublicFunction(ByVal value As Long) As Long
End Function

Friend Sub FriendSub(ByVal value As Long)
End Sub

Friend Function FriendFunction(ByVal value As Long) As Long
End Function

Then in a form module create a loop that executes each procedures a large number of times. For example, to see the difference in timing on a Pentium II machine you should perform about 1,000,000 calls to each routine. These are the timings I got:
For the Private class modules, one million iterations of either the Public Sub or Function routines took 0.46 seconds, while their Friend counterparts took only 0.05 and 0.06 seconds respectively (about 8-9 times faster). Timings are virtually the same with the MultiUse class module.

The probable explanation for this weird behavior is that Friend procedures don't undergo the overhead of marshaling and unmarshaling code. (Keep in mind that Public procedures can be called from outside the current project, in which case COM must marshal data back and forth.) While this explanation make sense, however, it doesn't explain while even Public routines in Private classes - that can't be called from outside the current program - show the same overhead. Moreover, relative timings don't change if the project type is changed to Standard EXE, where all classes are Private and can't be called through COM. In other words, it seems that there is some room for improving the code generated by the compiler.

The good news is that the overhead is really negligible in most cases, especially when the routine contains some complex and time-consuming statements.

Even though you should never select the Friend qualifier for performance reasons, keep in mind that Friend procedures have other advantages over Public ones, most notably the capability to accept and return UDT variables that are defined in a BAS module.

Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with, SQL Server and other MS technologies. He is, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )

Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.