Solved

inline in VB6

Posted on 2004-08-06
4
615 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

770 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