【问题标题】:Float Operations Performance C++ [duplicate]浮点运算性能 C++ [重复]
【发布时间】:2012-12-15 04:02:55
【问题描述】:

可能重复:
Floating point division vs floating point multiplication

最近,我写了一个程序来计算我的电脑需要多长时间 计算实数乘法、除法和加法。

为此,我使用了 QueryPerformanceFrequency 和 QueryPerformanceCounter 函数 为了获得时间间隔。

我已经使用 6,000,000 次迭代测试了我的程序:6000000 次乘法、除法和求和(使用浮点变量),并得到以下结果:

O.S = Windows Vista (TM) Home Premium, 32-bit (Service Pack 2)
Processor = Intel Core (TM)2 Quad CPU Q8200
Processor Freq = 2.33 GHz

Compiler = Visual C++ Express Edition


    nº iterations                              time in micro seconds
    6000000 x real    mult + assignment ->     15685.024214 us
    6000000 x real     div + assignment ->     51737.441490 us
    6000000 x real     sum + assignment ->     15448.471803 us
    6000000 x real           assignment ->     12987.614348 us

    nº iterations                              time in micro seconds 
    6000000 x real                mults ->      2697.409866 us
    6000000 x real                 divs ->     38749.827143 us
    6000000 x real                 sums ->      2460.857455 us

    1 Iteration                          time in nano seconds
    real                 mult ->         0.449568 ns
    real                  div ->         6.458305 ns
    real                  sum ->         0.410143 ns

是否有可能除法比乘法慢六倍,以及 加法实际上等于乘法(~ 0.42 ns)?

【问题讨论】:

  • 问:有没有可能除法比乘法慢六倍,而加法实际上等于乘法? 答: 是的。

标签: c++ performance performancecounter


【解决方案1】:

是的。如果您曾经手动进行过除法,那么您就知道它涉及到很多子操作。

至于加法和乘法的速度大致相同:它们更多地与设置操作数和存储结果有关,而不是实际计算。通过使用 SSE 指令进行加法和乘法编译(在 x86 上),您也许可以在它们之间获得更多差异。

【讨论】:

    【解决方案2】:

    使用现代 CPU,乘法与加法的速度大致相同。

    在英特尔的 Sandy Bridge 上,使用 SSE 进行乘法运算的时间是加法运算时间的 4/3。但是考虑到 ILP,可以同时处理多个指令,它们实际上需要相同的时间。在同一个 CPU 上,使用 ILP,单精度除法需要 7-14 倍的时间!

    来源:Agner Fog's instruction tables。在他的网站上有很多非常好的读物。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      相关资源
      最近更新 更多