Is Multiplying the Inverse Better or Worse?

前端 未结 11 1168
忘了有多久
忘了有多久 2020-12-20 13:52

When dealing with double data types is multiplying by the inverse better or worse?

Which way is faster? Which way uses

相关标签:
11条回答
  • 2020-12-20 14:33

    I would think the first method is clearly preferred because it is explicit. Imagine finding this in someone else's code. How are you sure that 0.00155... is really 1/645.16? What if the original programmer made a mistake? Furthermore, how do I know that 645.16 is the correct conversion factor? It's always best to not leave numbers compacted or represented in uniform for simplicity. The most basic example is as follows:

    //hours per day * days per year
    int hoursPerYear = 24*365;
    

    We can clearly see that this number is correct, but how would you know, offhand, that 8760 is the correct answer? If you need to perform many of these operations, you may want to preprocess the data to the correct form BEFORE entering your intensive calculations. In this manner, you won't need incredible efficiency, and the question becomes moot.

    0 讨论(0)
  • 2020-12-20 14:34

    The answer will depend on the architecture of the executing environment. In general, divisions are usually slightly more costly than multiplication on most processors.

    So unless this is actually a performance concern, I probably wouldn't worry about it. Pick the conversion factor that's more understandable.

    0 讨论(0)
  • 2020-12-20 14:44

    Regarding compilers performing the optimization to mult, they can optimize this (GCC does): SquareInches = MMSquared * (1 / 645.16).

    0 讨论(0)
  • 2020-12-20 14:45

    Which one is "faster" is indeed a cpu-specific issue, or at least how much faster is CPU specific, yes, division is typically seen as slower than multiplication. And of course all performance questions can be answered with "it depends."

    HOWEVER, if you'd asked which is "better" rather than which is faster there would be a clear answer, the more readable one is better. The performance improvement you're looking at is likely on the order of several clock cycles, so unless you're talking about doing this millions of times, you're trying to save yourself microseconds. And there is no microsecond optimization that is worth sacrificing readability and maintainability.

    0 讨论(0)
  • 2020-12-20 14:47

    from my VB code timer

    Dim SquareInches As Double
    Dim MMSquared As Double = 81
    Const d As Double = 645.16
    Const m As Double = 1 / 645.16
    Private Function TestCase1() As Boolean 'One
        'Test One Code Here
        SquareInches = MMSquared / d
        'end test code
        Return True
    End Function
    Private Function TestCase2() As Boolean 'Two
        'Test Two Code Here
        SquareInches = MMSquared * m
        'end test code
        Return True
    End Function
    

    results

         3/17/2009 2:13:27 PM   CPU - 1.794GHz
     One - Using Division
     Two - Using Multiplication
     Outer Loops(OL)=7  Inner Loops=262,144
      ( times in ticks.  1 ms. = 10,000 ticks )
     >> Two faster, 0.0488 ticks/loop
      Ticks / Loop
     0.0342         0.0819          0.0331          0.0488
     OL Base        One             Two              One - Two
     1   8,936          21,459          8,609           12,850
     2   9,008          21,416          8,682           12,734
     3   8,965          21,423          8,643           12,780
     4   8,964          21,457          8,659           12,798
     5   8,966          21,469          8,640           12,829
     6   8,987          21,660          8,688           12,972
     7   8,963          21,429          8,802           12,627
    
      Average
     8,969          21,473          8,674           12,799
      Variance
     431.4          6,160.3         3,315.9       
      Standard Deviation
     20.8           78.5            57.6          
     3/17/2009 2:13:27 PM
    
    0 讨论(0)
提交回复
热议问题