Announcement

Collapse
No announcement yet.

Multiply vs Divide, and use of Equates

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiply vs Divide, and use of Equates

    I'm still working on my super-big program. There are a lot of places where I do units conversions twice - first I multiply by 1.689, then later I must divide by 1.689. These are peppered throughout a highly iterative routine so I may hit them 50,000 times in a single analysis.

    Two Expert's Questions:

    1) Is it really much faster to multiply by 0.592 than to divide by 1.689?

    2) Should I create equates for 1.689 and 0.592 and use them rather than repeat those constant values wherever needed in the code? Ie., is it (a) faster and/or (b) smaller executable?

    Thanks, mighty online gurus!

    Dan

  • #2
    About the 1 I don't know, you might try measuring that with a loop with many iterations.

    About the 2, that should make no difference at all because equates work by text substitution when compiling. But equates are available only for integers.

    Comment


    • #3
      Dividing takes longer than multiplying. The following is in PBCC, not PBDOS:
      Code:
      %count = 100000     'number of iterations
      
      'From winAPI32
      Declare Function QueryPerformanceFrequency Lib "KERNEL32.DLL" Alias "QueryPerformanceFrequency" (lpFrequency As Quad) As Long
      Declare Function QueryPerformanceCounter   Lib "KERNEL32.DLL" Alias "QueryPerformanceCounter" (lpPerformanceCount As Quad) As Long
      
      Function PBMain
        Local Ctr1 As Quad
        Local Ctr2 As Quad
        Local Freq As Quad
        Local Overhead As Quad
        Local Time1 As Ext, Time2 As Ext
        Local i As Long, x As Single
      
        QueryPerformanceFrequency Freq
        QueryPerformanceCounter Ctr1      'find API overhead
        QueryPerformanceCounter Ctr2
        Overhead = Ctr2 - Ctr1
      
      '------------
        Print "Multiply:"
        QueryPerformanceCounter Ctr1
        For i = 1 To %count
         x = i*0.592
        Next
        QueryPerformanceCounter Ctr2
        Time1 = (Ctr2 - Ctr1 - overhead) / Freq
        Print "(" + Format$(Ctr2) + " - " + Format$(Ctr1) + " - " + Format$(Overhead) + ") / " + Format$(Freq)
        Print "= " + Format$(Time1, "##.0000") + " seconds"
        Print
      '------------
        Print "Divide:"
        QueryPerformanceCounter Ctr1
        For i = 1 To %count
         x = i/1.689
        Next
        QueryPerformanceCounter Ctr2
        Time2 = (Ctr2 - Ctr1 - overhead) / Freq
        Print "(" + Format$(Ctr2) + " - " + Format$(Ctr1) + " - " + Format$(Overhead) + ") / " + Format$(Freq)
        Print "= " + Format$(Time2, "##.0000") + " seconds"
        Print
      '------------
        Print "Divide time / Multiply time:"
        Print Time2/Time1
      
        WaitKey$
      End Function
      Last edited by Mark Hunter; 26 Oct 2007, 06:14 PM.
      Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

      Comment


      • #4
        Here's similar code for PBDOS
        Code:
        %count = 30000
        dim x as single
        dim timer1 as single
        dim timer2 as single
        
        cls
        
        print "Mutliply"
        mtimer
        for i = 1 to %count
         x = i*0.592
        next
        time1 = mtimer
        print time1;" microseconds"
        print
        
        print "Divide"
        mtimer
        for i = 1 to %count
         x = i/1.689
        next
        time2 = mtimer
        print time2;" microseconds"
        print
        
        print "Divide / Multiply"
        print time2/time1
        Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

        Comment


        • #5
          I'm not really a math type, but if you first have to multiply by 1.69, then divide by 1.69 later, do you really need to do either?
          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            At the cpu level, it is almost exactly three times slower to divide than multiply for a pentium 4.

            Comment


            • #7
              Why am I multiplying then dividing

              FYI, it's my big aircraft design program. Users like to input in knots (nautical miles per hour). All the equations use feet-per-second. So, gotta do units conversions.

              For users who like to use metric, all their inputs first get multiplied by that conversion factor.

              Per a previous thread of mine, sacraficing goats would be easier.

              Comment

              Working...
              X