Results 1 to 8 of 8

Thread: Optimizing code for speed

  1. #1

    Thread Starter
    Addicted Member jastrzebiec's Avatar
    Join Date
    Nov 2010
    Posts
    173

    Optimizing code for speed

    Hi,
    My code makes quite extensive calculations, and I would like to speed up the operations as fast as possible by eliminating one line of code or changing the dimmension of a variable.
    I appreciate you comments.
    Version 1.

    tempMemI(i) is just Integers array
    Dim Temp As Integer
    Dim Total As Long

    For i = 0 to MemLen
    If tempMemI(i) = -32768 Then
    Temp = 32767
    Else
    Temp = Abs(tempMemI(i))
    End If
    Total = Total + Temp
    Next
    AvgValue(BufferNo) = Total / TempBuff

    Version 2.
    Dim Temp As Long
    Dim Total As Long

    For i = 0 to MemLen
    Temp = Abs(tempMemI(i))
    Total = Total + Temp
    Next
    AvgValue(BufferNo) = Total / TempBuff

    Which version is better for speed calculation?
    Thanks,
    jas

    Added April 19th
    Apparently version 2 above does not work as I thought.
    Casting Temp as Long does not fix the problem.
    Just trying this in Immediate window:
    ?abs(tempMemI(i)) > where value of tempMemI(i) = -32768
    causes the overflow error.
    How to optimize that code for speed then?
    jas
    Last edited by jastrzebiec; Apr 19th, 2011 at 05:31 PM. Reason: new info

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Optimizing code for speed

    Just changing that variable from Integer to Long should make it faster, but only by a small amount.

    Also removing the If statement should make it faster, by a much larger amount. However, it is different behaviour so you should make sure it is valid for your situation.

    Eliminating the Temp variable (instead add the value directly to Total) will probably help too.

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Optimizing code for speed

    Make sure you have in Compile tab, behind Advanced optimizations button:
    • Remove array boundary check
    • Remove integer overflow check
    • Remove floating point check

    Having these ticked will dramatically reduce performance issues, but may also introduce new bugs if there is code that relies on error triggering.

    I can't recall how good performance Abs has and not in the mood for a benchmark. It should be quite straightforward function, but in case it isn't:
    Code:
    Dim Temp As Long
    Dim Total As Long
    
    For i = 0 to MemLen
        Temp = tempMemI(i)
        If Temp < 0 Then
            Total = Total - Temp
        Else
            Total = Total + Temp
        End If
    Next
    AvgValue(BufferNo) = Total / TempBuff
    May perform faster despite longer code.

    However, datatype coercing always introduces some speed decrease (in this case Integer -> Long, but you have to do it).

  4. #4

    Thread Starter
    Addicted Member jastrzebiec's Avatar
    Join Date
    Nov 2010
    Posts
    173

    Re: Optimizing code for speed

    Quote Originally Posted by Merri View Post
    Make sure you have in Compile tab, behind Advanced optimizations button:
    • Remove array boundary check
    • Remove integer overflow check
    • Remove floating point check

    Having these ticked will dramatically reduce performance issues, but may also introduce new bugs if there is code that relies on error triggering.
    What do you mean by that?
    Does that reduce or improve the performance?
    I have all those unchecked now.
    Thanks,
    jas

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Optimizing code for speed

    Check them. It'll vastly improve compiled code performance, because it reduces extra code which validates for errors, and is included by the compiler.

    Removing them, however, makes this code work:

    Code:
    Dim ABC(0) As Long
    ABC(1) = 1
    It will error in IDE, but works compiled when "Remove array boundary check" is checked, because additional code by compiler is not included which would check if index 1 is a valid index for that array.

    For this kind of reasons these "advanced optimizations" are dangerous and you must know what they do, and avoid writing code that depends on such errors.

  6. #6

    Thread Starter
    Addicted Member jastrzebiec's Avatar
    Join Date
    Nov 2010
    Posts
    173

    Re: Optimizing code for speed

    Thank you.
    It is always nice to learn something new.

    You said, that checking those options will only apply to compiled version.
    Of course.
    But, if everything works fine in IDE what can be a danger when using the compiled executable? Isn't IDE test not good enough?
    What can I miss?
    Thanks,
    jas

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Optimizing code for speed

    Running code from the IDE is a bit different to running a compiled App. The main differences being that the IDE always runs an unoptimised version of the code with extra bits to aid debugging.

    Generally speaking the IDE test is good enough. Merri is making the point that if those advanced optimisations are checked and your code relies on say an overflow error, it will work as expected in the IDE but it won't work as expected when compiled. This is particularly important to bear in mind when disabling Array Bound Checks.
    W o t . S i g

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Optimizing code for speed

    A general point to be made: if you want to write fast code, you have to take off the safe guards that protect against errors. Convenience is always lost and extra attention & skill needs to be put into every single detail. That is the cost of writing fast code.

    You can also achieve speed by writing good algorithms (code logic) that reduce the amount of processing. If you ever take a look at sorting algorithms you'll see how different algorithms solve performance issues. Then to really get your head hurt you can take a look at string sorting algorithms: they are complex and intelligent to gain optimal speed.

    Luckily most of the code that needs to be written works without massive optimization or attention to detail. Just keeping the information correct is enough. Unfortunatenaly when everything is coded using convenient methods (see the current Microsoft Live Messenger for an example) the performance of the resulting application is poor.

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