java 字元拼接效能 比拼

2021-05-23 00:33:48 字數 754 閱讀 6901

單執行緒測試

測試次數:5

測量單位:毫微秒

測試類:

迴圈新增次數:1000000(由於普通的string「+"字元操作1000000根本跑不動,所以用的是10000)

stringbuilder

cost time: 97973625

cost time: 84470233

cost time: 85545509

cost time: 84069902

cost time: 83813725

cost time: 95636457

cost time: 78379795

stringbuffer

cost time: 157424197

cost time: 138400272

cost time: 140522608

cost time: 144071663

cost time: 89715872

cost time: 103994782

cost time: 84376087

string

cost time: 653045010

cost time: 668044961

cost time: 631079928

cost time: 634437338

cost time: 639070049

cost time: 626573489

cost time: 635229338

常用String字串拼接的效能比較與分析

平時編寫 的過程中,免不了string字串的拼接,特別是需要拼接sql語句的時候 本人平時用的比較多的方式有 1 string1 string2 2 stringbuffer 3 string1.concat string2 為了放大效能差距 每種方式迴圈十萬次 方式1 test public vo...

字串拼接效能比較出乎意料的結果

一直認為string拼接大字串時用stringbuffer能比直接用 號效能更高,今天偶然發現不竟然,是有前提的。如果string物件沒用重新賦值,而是一次性把字串拼接好賦值給string變數時效能是最高的 第二個例子string2 如果string使用的是 重新賦值,這時效能確實要比stringb...

Java 中常用集合效能比較

list 集合 list 有順序的,元素可以重複 遍歷 for 迭代 排序 comparable comparator collections.sort arraylist 底層用陣列實現的list 特點 查詢效率高,增刪效率低 輕量級 執行緒不安全 遍歷 arraylistal new array...