Results 1 to 5 of 5

Thread: Real integer division in VB!!!

  1. #1

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Cool

    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:

    Code:
    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


    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  2. #2
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    355
    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)
    buzzwords are the language of fools

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    The \ integer division operator was one of the first things I learnt when I was learning VB, surely it's common knowledge?
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    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
    Sanity is a full time job

    Puh das war harter Stoff!

  5. #5

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width