» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

Use "x * x" instead of "x ^ 2"

You can use "x * x" instead of "x ^ 2". The end result will be the same. Well, I think the statistics prove themself, it's definetly worth to change your code to the optimized method! But As you can see the time for the calculations is very low anyways. Not even a second for 500.000 iterations Maybe with very large iterations you might notice it. Still I recommend to change the code since we want to have our programs running at the maximal speed possible, don't we? I'm not sure but maybe for larger quadratic calculations the routine might become faster. For example "x ^ 5" can also be done using "x * x * x * x * x"

Code:
Public Sub TestOne() Dim dblResult As Double dblResult = 4 ^ 2 End Sub Public Sub TestTwo() Dim dblResult As Double dblResult = 4 * 4 End Sub

x*x % faster than x^2 x*x (sec) x^2 (sec)
8606% 0,007162 0,623485
8427,3% 0,007151 0,609830
4308,6% 0,013844 0,610351
8368,1% 0,007201 0,609783
8567,5% 0,007018 0,608305


User contributed notes:

Author: Confused () Date: 11:05 15/05/2004
Shouldn't the % faster column say x*x % faster?
Really, the x^2 takes more time, meaning that it isn't faster, but slower...

Author: jap (jap) Date: 02:07 22/07/2004
faster=slower in tabel ?

Author: Almar Joling () Date: 09:07 22/07/2004
Better now? :)

Author: Spodi (spodii at hotmail dot com) Date: 02:08 17/08/2005
I have tried this out, and it seems that when using complex equations, using x * x can lead to overflows where x ^ 2 will not. An example of this is in:

SideA = Sqr(Abs(X1 - X2) ^ 2 + Y1 ^ 2)

Author: Tom (hurendo_kun at hotmail dot com) Date: 04:09 23/09/2005
I'm assuming "N ^ X" does its own implicit type conversion to avoid overflows. This would explain why it's so much slower than pure multiplication.

Author: Tom (hurendo_kun at hotmail dot com) Date: 19:10 21/10/2005
Okay, I did some more research on this. The return value of N^X is either a Double or a Variant containing a Double, to quote MSDN. So, there's definitely type conversion going on under the hood. I'd also remind you that the ^ operator can be used to derive roots, not just exponentiation, which makes it more useful in some cases.

Author: TheShau (shauros at walla dot com) Date: 14:08 04/08/2006
I don't think the type casts have much impact on the speed, at least not a linear impact. But I'm wondering if they did something like writing a single loop algorithm that computes exponentiation in general, including powers and roots. I think in that case the raising to a power operation will be much slower then in other languages and compilers. ... Just a thought.

Author: triggernum5 (born2killspam at gmail dot com) Date: 19:08 21/08/2006
a) Thankyou for not forcing me to register..:)
b) I just thought I'd mention that I did testing on vb6 arithmetic, and found that there is often a drastic (Up to 40% in extreme case) performance increase to be had by splitting long equations over multiple lines.. A good rule of thumb when you need fast math is to replace avoid the brackets and take it to the next line instead

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: