Solved

inline in VB6

Posted on 2004-08-06
4
617 Views
Last Modified: 2006-11-17
When you call a function several times it takes more time then you put it directly in the calling function.
In C++ there is an "inline" command. On compile time it insert the function in your calling function. Is there something in VB6 (or maybe an add-in)
0
Comment
Question by:dgb
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Daniel_Klann
ID: 11734620
Hi,

Unfortunately there's not an equivalent of the inline command.  If you have a function that is called many thousands (or millions) of times then you should test to see if you get better performance by putting the code inline.  It's a balancing act of making your code readable and well structured, whilst also maintaining acceptable levels of performance.

Daniel

0
 
LVL 2

Accepted Solution

by:
Daniel_Klann earned 250 total points
ID: 11734647
I did a simple test to see what the difference in performance was:-

'-------------------------------------------------------------------------------
'FUNCTION CALL

Private Sub Command1_Click()
    Dim lngLoop As Long, t As Single, y As Long

    t = Timer

    For lngLoop = 1 To 100000000
        y = MyFunction
    Next lngLoop

    MsgBox "Executed in " & Format(Timer - t, "0.00") & " seconds"

End Sub


Function MyFunction()
    MyFunction = 123
End Function

'-------------------------------------------------------------------------------
'CODE INLINE
Private Sub Command1_Click()
    Dim lngLoop As Long, t As Single, y As Long

    t = Timer

    For lngLoop = 1 To 100000000
        y = 123
    Next lngLoop

    MsgBox "Executed in " & Format(Timer - t, "0.00") & " seconds"

End Sub

The inline code above was about 18 times faster than the function code when run from within the VB IDE.  When I compiled each piece of code the inline code was about 50 times faster than the function call.  Obviously this is a simple example but demonstrates that in this case, the code second code would be preferable.

Cheers,
Dan



0
 
LVL 13

Assisted Solution

by:imarshad
imarshad earned 250 total points
ID: 11735623
Hi dqb,
          As stated earlier there is no equivalent to Inline in VB...... Main fact is that inline functions should never be large; they can be maximum 4-5 lines long. It is better to call the function rather than declare it as inline if it is larger than that, this rule is for C++. The same thing should be applicable to VB also. So you can use inline as suggested by Daniel while still it retains the readability of the code( 4-5 lines)........

Other Options are

Always use ByVal instead of ByRef for those functions that are called in aloop.....You may or may not know that in VB, ByRef variables are not passed like pointers in c++, they are pushed onto the stack before the function call; popped off, then pushed back on again before the function returns, just to be popped back off afterwards [basically, copied twice in memory]. This is of course twice as slow as ByVal where they are only copied once.......

YOu may use inline ASM code to speed up your code......
These links are highly recommended to be visited....

http://www.persistentrealities.com/vbfibre/index.php
http://www.persistentrealities.com/vbfibre/index.php?category=0&item=5&t=asm
http://www.persistentrealities.com/vbfibre/index.php?category=3&item=0
http://www.persistentrealities.com/vbfibre/index.php?category=2&item=0&t=asm

They contain some wonderful optimizing techniques......

Hope this helps......

Imran Arshad
0
 

Author Comment

by:dgb
ID: 11820130
Both of you answers combined is the answer i was looking for.
Thanks guys
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question