<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          String、StringBuiler、StringBuffer,誰(shuí)性能最高?

          共 1205字,需瀏覽 3分鐘

           ·

          2020-10-14 06:06

          Java技術(shù)棧

          www.javastack.cn

          關(guān)注閱讀更多優(yōu)質(zhì)文章



          作者:Yuchao Huang

          來(lái)源:www.cnblogs.com/misterchaos/p/12782986.html


          一、前言

          剛開始學(xué)習(xí)Java時(shí),作為只會(huì)C語(yǔ)言的小白,就為其中的字符串操作而感到震撼。

          相比之下,C語(yǔ)言在字節(jié)數(shù)組中保存一個(gè)結(jié)尾的\0去表示字符串,想實(shí)現(xiàn)字符串拼接,還需要調(diào)用strcpy庫(kù)函數(shù)或者自己手動(dòng)去復(fù)制數(shù)組,非常麻煩,更別提其他復(fù)雜操作。

          而Java通過String類讓字符串操作變得十分簡(jiǎn)單和方便。除此之外,還有stringbuilder等這些類的輔助,那么本文就String,StringBuiler和StringBuffer的區(qū)別開始,去探討Java中的字符串操作

          二、String,StringBuiler和StringBuffer

          2.1 String類

          Java 提供了 String 類來(lái)創(chuàng)建和操作字符串。關(guān)注公眾號(hào)Java技術(shù)棧回復(fù)面試獲取系列面試題。

          在源碼中可以看到,String類內(nèi)部的實(shí)現(xiàn)也是一個(gè)字節(jié)數(shù)組,這個(gè)數(shù)組是final類型的,因此String是不可變的對(duì)象,每次在對(duì)String類進(jìn)行改變的時(shí)候都會(huì)生成一個(gè)新的string對(duì)象,然后將指針指向新的string對(duì)象。

          推薦閱讀《5 個(gè)刁鉆的 String 面試題!

          2.2 StringBuiler 類

          和 String 類不同的是,StringBuilder 類的對(duì)象能夠被多次的修改,并且不產(chǎn)生新的對(duì)象

          這個(gè)特性的意義在于,如果我們進(jìn)行大量的字符串操作,使用String類就會(huì)產(chǎn)生很大的性能消耗,而StringBuilder就可以避免這個(gè)問題。

          2.3 StringBuffer 類

          StringBuffer 和StringBuiler之間的最大不同在于 StringBuilder 的方法不是線程安全的

          由于 StringBuilder 相較于 StringBuffer 有速度優(yōu)勢(shì),所以多數(shù)情況下建議使用 StringBuilder 類。然而在應(yīng)用程序要求線程安全的情況下,則必須使用 StringBuffer 類。

          2.4 String,StringBuiler和StringBuffer的比較(對(duì)比C/C++)


          三、各種字符串操作的效率測(cè)試

          3.1 測(cè)試代碼

          @Test
          public?void?test()?{
          ????int?count?=?100000;

          ????long?startTime?=?System.currentTimeMillis();
          ????String?str?=?"";
          ????for(int?i?=?0;?i????????str?+=?i;
          ????}
          ????System.out.println("執(zhí)行"+count+"次??String?耗時(shí):"+?getRunTime(startTime));

          ????startTime?=?System.currentTimeMillis();
          ????StringBuilder?stringBuilder?=?new?StringBuilder("");
          ????for?(int?i?=?0;?i?????????stringBuilder.append(i);
          ????}
          ????System.out.println("執(zhí)行"+count+"次??StringBuilder?耗時(shí):"+?getRunTime(startTime));

          ????startTime?=?System.currentTimeMillis();
          ????StringBuffer?stringBuffer?=?new?StringBuffer("");
          ????for?(int?i?=?0;?i?????????stringBuffer.append(i);
          ????}
          ????System.out.println("執(zhí)行"+count+"次??StringBuffer?耗時(shí):"+?getRunTime(startTime));

          }

          3.2 測(cè)試結(jié)果

          執(zhí)行100000次??String?耗時(shí):32s
          執(zhí)行100000次??StringBuilder?耗時(shí):2ms
          執(zhí)行100000次??StringBuffer?耗時(shí):4ms

          3.3 小結(jié)

          可以看到String類的性能遠(yuǎn)低于StringBuiler和StringBuffer,而StringBuiler在本次測(cè)試中比Stringbuffer提高了50%的性能

          四、Java字符串和正則表達(dá)式

          4.1 測(cè)試代碼

          @Test
          public?void?test0(){
          ????//郵政編碼
          ????String?postCode?=?"[1-9]\\d{5}";
          ????//區(qū)號(hào)-座機(jī)號(hào)碼
          ????String?areaCode?=?"\\d{3}-\\d{8}|\\d{4}-\\d{7}";
          ????//手機(jī)號(hào)碼
          ????String?phone?=?"(?:13\\d|15\\d|18\\d)\\d{5}(\\d{3}|\\*{3})";

          ????String?text?=?"郵政編碼:440834"+
          ??????????????????"區(qū)號(hào)-座機(jī)號(hào)碼:?020-12345678"+
          ??????????????????"手機(jī)號(hào):13536373839"+
          ??????????????????"郵政編碼:440833"+
          ??????????????????"區(qū)號(hào)-座機(jī)號(hào)碼:?010-12345678"+
          ??????????????????"手機(jī)號(hào):13536373739";

          ????Pattern?p?=?Pattern.compile(postCode);
          ????Matcher?m?=?p.matcher(text);
          ????System.out.println("文本中包含郵政編碼:");
          ????while?(m.find()){
          ????????System.out.println(m.group());
          ????}

          ????p?=?Pattern.compile(areaCode);
          ????m=?p.matcher(text);
          ????System.out.println("文本中包含區(qū)號(hào)-座機(jī)號(hào)碼:");
          ????while?(m.find()){
          ????????System.out.println(m.group());
          ????}

          ????p?=?Pattern.compile(phone);
          ????m=?p.matcher(text);
          ????System.out.println("文本中包含手機(jī)號(hào):");
          ????while?(m.find()){
          ????????System.out.println(m.group());
          ????}
          }

          4.2 測(cè)試結(jié)果

          文本中包含郵政編碼:
          440834
          123456
          135363
          440833
          123456
          135363
          文本中包含區(qū)號(hào)-座機(jī)號(hào)碼:
          020-12345678
          010-12345678
          文本中包含手機(jī)號(hào):
          13536373839
          13536373739

          五、總結(jié)

          經(jīng)過測(cè)試和比較,可以看到Java中同為字符串操作,但由于背后實(shí)現(xiàn)的原理不同,形成的性能差異也是十分巨大,相比之下,C/C++中的字符串操作性能更高。

          String類的性能遠(yuǎn)低于StringBuiler和StringBuffer,而StringBuiler比Stringbuffer的性能稍微高一點(diǎn)。對(duì)性能的探究,最終還是要回到使用場(chǎng)景。

          可以總結(jié)得出,如果不涉及字符串操作,那么String類是首選,如果涉及的字符串操作沒有線程安全問題,那么使用StringBuilder,如果涉及的字符串操作存在線程安全問題,那么使用StringBuffer





          關(guān)注Java技術(shù)棧看更多干貨



          戳原文,獲取精選面試題!
          瀏覽 81
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  翔田千里熟妇息孑交尾 | 欧美精品成人在线视频 | 日本三级中国三级99 | 亚欧美在线视频中文字幕 | 亚洲精品字幕久久久久 |