Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

inline in VB6

Posted on 2004-08-06
4
Medium Priority
?
624 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 1000 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

972 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