Solved

inline in VB6

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now