Solved

i want to know whether ordinery function slows down a appplication

Posted on 2004-10-01
4
185 Views
Last Modified: 2013-11-13
Hi experts,
 my question is that iam having a lot of function in my form to reduce complexity and have genral flexibility i placed them in modules.. the think i want to know is does this function  really help in speeding upof a application as it is reducing complexity code wise.... so i want to know having more function is help ful or not

2.. i like have some sample codes for function passing mutilple parameters and returning multiple parameters

looking for  a solution


regards
vairavamuthu
0
Comment
Question by:vairavamuthu
4 Comments
 
LVL 3

Accepted Solution

by:
sgayatri earned 20 total points
ID: 12198610
You can use procedures or functions for reducing complexity in code and better readability.
Functions always return single value.
for ex:
private function abc () as string

abc = "xxxx"
end function

If you want a function to return values
then you must declare them as byref parameters
In General declarations,
dim x as integer
dim y as string
dim func_val as string
...
...
...
''' somewhere in your code call the function abc
x=10
y="somestring"

func_val = abc(x,y)  '' you are passing x, y to function called abc

private function abc (byref x as integer, byref y as string) as string

''' use x and y here
...
...
''' change x,y here
x=12
y="someotherstring"
abc = "xxxx"
end function

Hope I am clear in my explanation

0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 12199283
Functions reduce complexity, but slightly slows down your application.

VB is not the fastest language in the world, and if your app is performance critical, VB is not a great choice, but if your application is otherwise fine, but you have one bit that is too slow, you might be able to speed it up.  For example, look at this code, a simple loop doing simple math, one way in a function, the other with inline code.  The function loop takes about 16 seconds on my machine, the inline loop takes 2 seconds.  e.g. each function call takes about 0.00000056 seconds.  Not much, but it adds up if you are doing 25,000,000 calls.

Option Explicit

Private Sub CmdWithFunction_Click()
    Dim starttime As Date
    Dim stoptime As Date
    Dim i As Integer, j As Integer
    Dim result As Long
    Screen.MousePointer = vbHourglass
    starttime = Time
        For i = 1 To 5000
            For j = 1 To 5000
                result = MyFunction(i, j)
            Next j
        Next i
    stoptime = Time
    Screen.MousePointer = vbDefault
    MsgBox DateDiff("s", starttime, stoptime)
End Sub

Function MyFunction(i As Integer, j As Integer) As Long
    MyFunction = i + j
End Function

Private Sub CmdNoFunction_Click()
    Dim starttime As Date
    Dim stoptime As Date
    Dim i As Integer, j As Integer
    Dim result As Long
    Screen.MousePointer = vbHourglass
    starttime = Time
        For i = 1 To 5000
            For j = 1 To 5000
                result = i + j
            Next j
        Next i
    stoptime = Time
    Screen.MousePointer = vbDefault
    MsgBox DateDiff("s", starttime, stoptime)

End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

932 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

10 Experts available now in Live!

Get 1:1 Help Now