Since VBScript hides the details of data types so well, sometimes it's easy for a VBScript programmer to forget that the actual storage of a number comes in many different flavours 'behind the scenes'. In VBScript you normally use the / (division) operator to divide one number from another. However, there is an alternative to the normal division operator that may be beneficial to your application, depending on your needs.

First, if you are certain at design time that you will always be dividing integers (numbers with no decimal) AND you wish to recieve an integer output (rounded off, if there are decimals) you can speed up the calculation a little bit by using the integer division operator (\). Since division is already an extremely quick feature most languages, the approximate 10% gain (by my calulation) made by using the integer division operator is quite small. I timed 5 million (5000 000) division operations of random integers at between 9 and 10 seconds. Unless you are doing large numbers of division operations, this gain may not be important to you, however the 10% gain increases to about 25% if the quotient (the output of your division) happens to also be an integer (i.e. 12 \ 2 = 6).

Compare:

' regular division ' @ 10 seconds: For i = 1 to 500000 intQuotient = 100 / 3 ' intQuotient = 33.333333... Next

' integer divison operator ' @ about 9 seconds: For i = 1 to 500000 intQuotient = 100 \ 3 ' intQuotient = 33 Next

' integer division w/ natural integer output: ' @ about 7.5 seconds For i = 1 to 500000 intQuotient = 100 \ 2 ' intQuotient = 20 Next

Second, if you have floating point number which you wish to round before dividing, the integer division operator automatically does this; consider it a 'side-effect' which you may find useful. In other words plugging numbers with decimals into the integer division operator is the equivalent of this:

Round(Round(numerator) / Round(denominator))

Where numerator is the number being divided and denominator is the number you are dividing by. This is a very specific case but if it's appropriate to what you are doing, the integer division operator will be much faster than the normal, floating point operator.

FAQ posted by Richard Lowe at
1/14/2001 2:36:27 AM to the
Math Functions category.
This FAQ has been viewed 55,681 times.

Do you have a FAQ you'd like to suggest?
Suggestions? Comments? If so, send it in!
Also, if you'd like to be a FAQ Admin (creating/editing FAQs),
let me know! If you are looking for other FAQs, be
sure to check out the 4Guys
FAQ and Commonly Asked Messageboard Questions!