<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## Chapter 9. General Programming(通用程序設計) ### Item 63: Beware the performance of string concatenation(當心字符串連接引起的性能問題) The string concatenation operator (+) is a convenient way to combine a few strings into one. It is fine for generating a single line of output or constructing the string representation of a small, fixed-size object, but it does not scale. Using **the string concatenation operator repeatedly to concatenate n strings requires time quadratic in n.** This is an unfortunate consequence of the fact that strings are immutable (Item 17). When two strings are concatenated, the contents of both are copied. 字符串連接操作符 `(+)` 是將幾個字符串組合成一個字符串的簡便方法。對于生成單行輸出或構造一個小的、固定大小的對象的字符串表示形式,它是可以的,但是它不能伸縮。使用 **字符串串聯運算符重復串聯 n 個字符串需要 n 的平方級時間。** 這是字符串不可變這一事實導致的結果([Item-17](/Chapter-4/Chapter-4-Item-17-Minimize-mutability.md))。當連接兩個字符串時,將復制這兩個字符串的內容。 For example, consider this method, which constructs the string representation of a billing statement by repeatedly concatenating a line for each item: 例如,考慮這個方法,它通過將每個賬單項目重復連接到一行來構造賬單語句的字符串表示: ``` // Inappropriate use of string concatenation - Performs poorly! public String statement() { String result = ""; for (int i = 0; i < numItems(); i++) result += lineForItem(i); // String concatenation return result; } ``` The method performs abysmally if the number of items is large. **To achieve acceptable performance, use a StringBuilder in place of a String** to store the statement under construction: 如果項的數量很大,則該方法的性能非常糟糕。**要獲得能接受的性能,請使用 StringBuilder 代替 String** 來存儲正在構建的語句: ``` public String statement() { StringBuilder b = new StringBuilder(numItems() * LINE_WIDTH); for (int i = 0; i < numItems(); i++) b.append(lineForItem(i)); return b.toString(); } ``` A lot of work has gone into making string concatenation faster since Java 6, but the difference in the performance of the two methods is still dramatic: If numItems returns 100 and lineForItem returns an 80-character string, the second method runs 6.5 times faster than the first on my machine. Because the first method is quadratic in the number of items and the second is linear, the performance difference gets much larger as the number of items grows. Note that the second method preallocates a StringBuilder large enough to hold the entire result, eliminating the need for automatic growth. Even if it is detuned to use a default-sized StringBuilder, it is still 5.5 times faster than the first method. 自 Java 6 以來,為了使字符串連接更快,已經做了大量工作,但是這兩個方法在性能上的差異仍然很大:如果 numItems 返回 100,lineForItem 返回 80 個字符串,那么第二個方法在我的機器上運行的速度是第一個方法的 6.5 倍。由于第一種方法在項目數量上是平方級的,而第二種方法是線性的,所以隨著項目數量的增加,性能差異會變得越來越大。注意,第二個方法預先分配了一個足夠大的 StringBuilder 來保存整個結果,從而消除了自動增長的需要。即使使用默認大小的 StringBuilder,它仍然比第一個方法快 5.5 倍。 The moral is simple: **Don’t use the string concatenation operator to combine more than a few strings** unless performance is irrelevant. Use StringBuilder’s append method instead. Alternatively, use a character array, or process the strings one at a time instead of combining them. 道理很簡單:**不要使用字符串連接操作符合并多個字符串**,除非性能無關緊要。否則使用 StringBuilder 的 append 方法。或者,使用字符數組,再或者一次只處理一個字符串,而不是組合它們。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-9/Chapter-9-Introduction.md)** - **Previous Item(上一條目):[Item 62: Avoid strings where other types are more appropriate(其他類型更合適時應避免使用字符串)](/Chapter-9/Chapter-9-Item-62-Avoid-strings-where-other-types-are-more-appropriate.md)** - **Next Item(下一條目):[Item 64: Refer to objects by their interfaces(通過接口引用對象)](/Chapter-9/Chapter-9-Item-64-Refer-to-objects-by-their-interfaces.md)**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看