Results 1 to 23 of 23

Thread: speed question

  1. #1

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945

    speed question

    I have a small procedure that is being run VERY often (@5Hz) and it currently resides in a module outside of the form that calls it. Would it be faster if I moved it into the code of the form?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Yes, procedure calls take time. Any parameters need to be passed/copied, and the code flow needs to jump (and jump back again afterwards).

    I once improved the speed of some code using this method, it went from doing 200 iterations per second to over 2000. (results will vary depending on the code involved).

    Obviously it may mean having to write the same code in more than one place, but if you are after speed it is an appropriate thing to do.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    A call is a call is a call. Doesn't matter where it is (unless it's in a separate object such as a dll or some such). If it's in a code module in the same project, moving it isn't going to help. Maybe what you should do is take a second look at it and see if there is a way to optimize it to run better.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    T.G. is right - I meant to say that you should put the code inline (ie: dont have it as a procedure)... browser crashed mid post tho


    There are many ways of optimising code, but it all depends on what you are doing, and how you are doing it. If you post the code (and an explanation of what it needs to do) we can give better advice.

    Putting procedures inline does give a speed boost, but it may be more hassle than it is worth, as you can easily get un-readble and hard-to-maintain code.

  5. #5

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Here is the code:
    VB Code:
    1. Public Sub GetStepTime()
    2. Dim lngTemp As Long
    3.  
    4.    On Error GoTo GetStepTime_Error
    5.  
    6.     If NEWSTEP Then
    7.         STARTSTEPTIME = GetTickCount
    8.         lngTemp = frmMain.flxSP2.CellText(STEPNUM, 4)
    9.         FULLSTEPTIME = lngTemp * 1000
    10.         NEWSTEP = False
    11.     Else
    12.         CURSTEPTIME = GetTickCount
    13.         If CURSTEPTIME - STARTSTEPTIME > FULLSTEPTIME Then
    14.             NEWSTEP = True
    15.             STEPNUM = STEPNUM + 1
    16.         End If
    17.     End If
    18.  
    19.    On Error GoTo 0
    20.    Exit Sub
    21.  
    22. GetStepTime_Error:
    23.  
    24.     LogError Err.Number, Err.Description, Err.Source, False, "GetStepTime of Module basDurb"
    25. End Sub
    It is checking to see if the time for a step in the program has expired. I have a timer on my main form that is set to 200ms that calls it. It seems to be working ok so far, but I still have a lot of functionality to build into the system (limit checking, data logging, value display, etc) and I want to make sure this runs as clean as possible.

    One thing that I was going to do to try and optomize it is to put the times into an array so that I'm not reading the value from a control on every pass.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Timer controls are not very good - apparently they are only accurate to about 70ms (that's up to 35% out with 200ms). They also have various other 'issues', as detailed in many posts on this forum.

    An API timer would be a far more accurate method (if you are concerned with a specific frequency, this can only be a good thing).



    You are right that reading from a control is slow, what is the reason for having it? (I don't understand from what you have already posted).

  7. #7

  8. #8

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Originally posted by si_the_geek
    An API timer would be a far more accurate method (if you are concerned with a specific frequency, this can only be a good thing).

    You are right that reading from a control is slow, what is the reason for having it? (I don't understand from what you have already posted).
    Can you recommend a good API timer? Should I use the SetTimer API? The only reason I was reading the value from a control is that I was going to let the user change the step time while running the test.... however, this is not necessary and would be far from the norm, so I guess reading it from an array would work just as easily.

    Martin, I will change those to constants, thank you.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  9. #9

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Ok, another question... each "step" I have been referring to has 4 parts and I'll have to refer to each part when the step is changes, so I might as well load them all into arrays..... so would it be faster to have a UDT of arrays, a 2 dimensional array, or 4 seperate arrays? The 4th part of each step will be hammered on much more than the other parts.

    In other words, I'll be checking the 4th element to see if that much time has passed, and then I'll be loading the other 3 parts for the next step when that time has expired.

    Is this making sense?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Martin,

    I was not aware that "literal" values in VB were not compiled into constants in the .EXE. Are you saying that having the value 1 in a math equation is slower than using a constant that is assigned to the value 1?

    Why?

  11. #11
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    ^ What he said. That would seem idiotic if it's the case.
    an ending

  12. #12

    Thread Starter
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    I would also like elaboration on this topic.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    All I can do is to quote from MSDN which says under the Optimizing for Speed topic

    Use Constants Whenever Possible
    Using constants makes your application run faster. Constants also make your code more readable and easier to maintain. If there are strings or numbers in your code that don’t change, declare them as constants. Constants are resolved once when your program is compiled, with the appropriate value written into the code. With variables, however, each time the application runs and finds a variable, it needs to get the current value of the variable.

    Whenever possible, use the intrinsic constants listed in the Object Browser rather than creating your own. You don’t need to worry about including modules that contain unused constants in your application; when you make an .exe file, unused constants are removed.
    Maybe it's really talking about variables but I don't know.

  14. #14
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    I think it means don't do something like:

    VB Code:
    1. Dim bla as long
    2.  
    3. bla = 5
    4.  
    5. Dim i as long
    6.  
    7. Dim total as long
    8.  
    9. for i = 0 to 10
    10.  
    11. total = total + bla
    12.  
    13. next

    Clearly "bla" is unecessary and that code might cause more fetches from memory. It's a bit of a no-brainer though.
    an ending

  15. #15
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I just found that substituting a constant for a number is faster. Here is the code that I ran as an exe.

    VB Code:
    1. Dim lngStart As Long
    2.     Dim lngFinish As Long
    3.     Dim lngCounterOne As Long
    4.     Dim lngCounterTwo As Long
    5.     Const FIVE As Integer = 5
    6.    
    7.     ' Record the start "time"
    8.     lngStart = GetTickCount()
    9.    
    10.     ' Some process that you want to time
    11.     For lngCounterOne = 1 To 9000000
    12.         lngCounterTwo = lngCounterTwo + FIVE
    13.     Next lngCounterOne
    14.    
    15.     ' Record the finish "time"
    16.    
    17.     lngFinish = GetTickCount()
    18.    
    19.     ' Display the difference
    20.     MsgBox CStr(lngFinish - lngStart)
    21.  
    22.     lngStart = GetTickCount()
    23.    
    24.     ' Some process that you want to time
    25.     For lngCounterOne = 1 To 9000000
    26.         lngCounterTwo = lngCounterTwo + 5
    27.     Next lngCounterOne
    28.    
    29.     ' Record the finish "time"
    30.    
    31.     lngFinish = GetTickCount()
    32.    
    33.     ' Display the difference
    34.     MsgBox CStr(lngFinish - lngStart)
    On my PC the code using FIVE took 16 ms while the code using 5 took 31 ms.

  16. #16
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    That's pretty strange, it looks like VB does behave like you said. Try more iterations though - GTC's resolution is only ~30ms.
    an ending

  17. #17

  18. #18
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548
    CStopWatch Class by Karl Moore


    This is an excellent class you can add to your project and call - great example to show you how to use it....

  19. #19
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    QueryPeformanceCounter is better But that aside, I tested the code on my PC and I'm seeing the opposite - the "5" code runs consistently faster.
    Last edited by azteched; Jun 9th, 2004 at 03:57 AM.
    an ending

  20. #20
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    MartinLiss: to get more accurate results, please make two different projects and EXEs. I've seen VB giving weird results when you run two test after each other in one code. As separate I've got much more accurate results.

  21. #21
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    Update: using QueryPerformanceCounter and seprarating out the two tests, I'm seeing no difference in performance between the pieces of code. This is with 900000000 iterations and averaging over 10 runs.
    an ending

  22. #22
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Yup, was as I thought: two tests in the same EXE is unreliable. Maybe this can be skipped in one project by making the tests behind separate command buttons. Thought it'd be plain silly if you had to use declared constants instead of constant numbers.

  23. #23
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    Yeh I put the tests in two functions behind two command buttons. It's not completely ideal, because the test you run first always runs about ~2ms slower, probably because of caching, but the whichever you run first, it's always the case, so the performance is identical.
    an ending

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width