PDA

Click to See Complete Forum and Search --> : Real integer division in VB!!!


kedaman
Nov 21st, 2000, 02:33 AM
It has been posted on a thread that \ does integer division, forgot where the thread was... anyway somehow it seemed like it didn't operate as integer division should and was rejected, well i just checked it out, and \ really does integer division. The problem was that the nonintegers that was involved was of course converted to integers using Cint() not Int() as you didn't expect.

However i did some test with both operators and the first thought i had was a bit disappointed:

2670 ms for integer division
2515 ms for floating point division

Thats not much difference, and when i saw that in fact the integer division was slower, i got suspicious did some new tests:


Private Declare Function GetTickCount Lib "kernel32" () As Long

Sub test()
Dim start&, a&
start = GetTickCount
For x = 0 To 10000000
'Here's the buggy tests, what vb does is uses CINT function on 23.3
'which should take up a lot of cpu speed as you see.
'a = 23.3 \ 1203 ' 2660 ms
'a = 23.3 / 1203 ' 2450 ms
'The winner of the contest is of course simple integer division
'Try to avoid floating point division whenever possible
'a = 23 \ 1203 ' 1504 ms
'a = 23 / 1203 ' 2940 ms
'Same goes for declared variables, integers are faster than floating points
'somehow you see the difference at floating point division, it's slightly faster
'when you leave the long declaration.
'a = 23& \ 1203& ' 1504 ms
'a = 23& / 1203& ' 2970 ms
'As you see integer division is useless with floating points, especially when you
'convert them. Also you can gain something on declaring floating points for floating
'point division
'a = 23! \ 1203! ' 3730 ms
'a = 23! / 1203! ' 2870 ms
'Doubles gives you a strange difference in opposite direction for both integer and
'floating point division
'a = 23# \ 1203# ' 3700 ms
'a = 23# / 1203# ' 2890 ms
'smaller numbers doesn't operate faster, it seems like the smaller the result is, the
'faster it operates.
'a = 5& \ 2& ' 1523 ms
'a = 5& / 2& ' 3693 ms
'Using Int to convert isn't good. it's better than leaving VB to decide Cint to do it
'but if you need to divide floating points with integer division repeatedly you should
'store the converted values somewhere.
'a = Int(23.3) \ Int(1203) ' 3030 ms


Next x
Debug.Print GetTickCount - start
End Sub

phew that was very unusual of me, commenting that much. Well hope this helps you with
all speed critical issues :)

KENNNY
Nov 21st, 2000, 02:51 PM
mmm interesting :)
problem is, in GL and D3D you need a bit of floating point, so you could run into speed probs if doing a lot.
vb really shouldn't let you use \ (int div) for something like 23.3 divided by 1203, or at least have a compiler option to disallow it, because as the test shows, it means slowdown, and you really should know the difference :)
(although i only realised there were 2 div operators a few months ago)

HarryW
Nov 21st, 2000, 05:51 PM
The \ integer division operator was one of the first things I learnt when I was learning VB, surely it's common knowledge?

/\/\isanThr0p
Nov 21st, 2000, 09:12 PM
I did quite a few programming, and today when I read this posts, I first saw this!!
I always used the other one!! Good to know!

Cu

kedaman
Nov 21st, 2000, 09:21 PM
well i've seen this in some other languages never heard it in basic though, anyway as you see you can get quite optimized using both \ and / in different situations :)