[細節] += & +

來源:互聯網
上載者:User

 某些演算法函數最後幾行操作都是一些數學運算,例如+ -。

這時候就有一個細節的問題了,比方說有一個加法函數:

public static long plus( long a, long b )                                                                                                                                   

函數體可以寫成:

return a + b;

也可以寫成:

a += b; return a;

那麼,那種效率更高?

一下是用IL DASE分析的2個函數的結果:

前者:

  // 代碼大小       9 (0x9)
  .maxstack  2
  .locals init (int32 V_0)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldarg.1
  IL_0003:  add
  IL_0004:  stloc.0
  IL_0005:  br.s       IL_0007
  IL_0007:  ldloc.0
  IL_0008:  ret

後者:

  // 代碼大小       12 (0xc)
  .maxstack  2
  .locals init (int32 V_0)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldarg.1
  IL_0003:  add
  IL_0004:  starg.s    a
  IL_0006:  ldarg.0
  IL_0007:  stloc.0
  IL_0008:  br.s       IL_000a
  IL_000a:  ldloc.0
  IL_000b:  ret

 

之後我就不說了~~~~~~因為~~~~~~~~我~~我~~~~實在是不知道~~~~~~哪個更有效率~~~~~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.