-
Apr 14th, 2011, 11:43 AM
#1
Thread Starter
Addicted Member
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
-
Apr 14th, 2011, 12:06 PM
#2
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.
-
Apr 15th, 2011, 10:45 AM
#3
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).
-
Apr 15th, 2011, 01:52 PM
#4
Thread Starter
Addicted Member
Re: Optimizing code for speed
Originally Posted by Merri
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
-
Apr 15th, 2011, 01:57 PM
#5
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.
-
Apr 15th, 2011, 04:41 PM
#6
Thread Starter
Addicted Member
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
-
Apr 16th, 2011, 12:38 AM
#7
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.
-
Apr 16th, 2011, 11:12 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|