?
Solved

inline in VB6

Posted on 2004-08-06
4
Medium Priority
?
620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses

771 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