Simplify the Math VB Help

Now we’re going to optimize the code in the loop. There isn’t much you can do other than to replace tl}eAoperator, which raises a number (integer and ‘non-integer) to a power. Raising a number to a non-integer power is accomplished with a rather complicated algorithm, but you can also do this by squaring or cubing a number, which is a far simpler operation. You can square a number by simply multiplying . it by itself. Many programmers will use the proper operator (xA2)to calculate the square of x because they don’t know better, or in most cases, because they are translating a math formula into Visual Basic statements. Start a “ew project, place a Command button on the Form, and insert the following code in its Click event handler.

This subroutine consists of two loops which calculate squares with two different methods. The first loop uses the-‘A operator, while the second loop multiplies the two numbers. Both loops use double variables (I couldn’t ignore the conclusions of the previous section, could I?) and the loop’s counter is a long variable. If you run this application now in the IDE, the results will probably surprise you. The A operator will take 11.430 seconds, while the multiplication will take 1.867 .seconds. This difference suggests that you should avoid the A operator whenever possible. Likewise, don’t use the A operator to calculate the square root, as in the follow,ing statement:

X2 – X ” 0.5

Use the Sqr() function instead, which is much faster:

X2 – Sqr(X)

Now that you’ve learned these basic math tricks, you can combine them in more complicated ef’pressions. For instance, the expression X”2 .5 is equivalent theexpression X*XIrSqr(X), but the second expression is evaluated mo re than twice as fast. Change the body of the first loop in the previous example to the following code segment:

dl – 1342.34434S456
d2 – dl ” 2.5
dl – 8856452#
d2 – d1^ .2.5

Change the second loop to the following:

d1 = 1342.344345456
d2 = dl * dl * Sqr(d1)
dl = 8856452#
d2 =  d1 * d1 * Sqr(d1)

Even in the Visual Basic IDE, the second (optimized) los>pwill complete its execution in 4.1 seconds and the first loop will finish in 9.5 seconds. This trick suggests that you should scrutinize the math expressions you’re coding. Don’t just translate . the math into Visual Basic statements. Rearrange terms, arid whenever possible; try to simplify them to help the compiler. Another way to optimize code that implements math operations is to save intermediate results. For example, the following statements

Xnew – X * X + XO: Ynew – Y * Y + YO
(other calculations)
If X *·X + Y * Y> 0 Then ….

will execute faster if you save the results of the operation X*X to a temporary variable, as shown next:

XSqua~e – X * X : YSquare a Y * Y
Xnew – Xsquare + XO: Ynew m Ysquare + YO
(other calculations)
If  Xsquare + Ysquare > 0 Then …

The improvement will be more impressive if you store more complicated operations, such as X”2. 8 or y”-o. 3, to temporary variables and reuse them.

Posted on November 6, 2015 in Optimizing VB Applications

Share the Story

Back to Top
Share This