Commons Lang
  1. Commons Lang
  2. LANG-250

[lang][PATCH] Performance boost for RandomStringUtils

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: Other

      Description

      This patch replaces the StringBuffer used in RandomStringUtils with a char
      array, resulting in a significant performance increase (better than 30% by my
      testing). It doesn't change the existing API, and it passes the provided unit
      tests.

      Thanks,
      Shaun Kalley

      Index: src/java/org/apache/commons/lang/RandomStringUtils.java
      ===================================================================
      — src/java/org/apache/commons/lang/RandomStringUtils.java (revision 179476)
      +++ src/java/org/apache/commons/lang/RandomStringUtils.java (working copy)
      @@ -230,26 +230,25 @@
      }
      }

      • StringBuffer buffer = new StringBuffer();
        + char[] buffer = new char[count];
        int gap = end - start;
      • while (count-- != 0) {
        + for (int i = 0; i < count; i++) {
        char ch;
        if (chars == null) { ch = (char) (random.nextInt(gap) + start); }

        else

        { ch = chars[random.nextInt(gap) + start]; }
      • if ((letters && numbers && Character.isLetterOrDigit(ch))
      • (letters && Character.isLetter(ch))
        + if ((letters && Character.isLetter(ch))
        (numbers && Character.isDigit(ch))
        (!letters && !numbers)) { - buffer.append(ch); + buffer[i] = ch; }

        else

        { - count++; + i--; }

        }

      • return buffer.toString();
        + return new String(buffer);
        }

      /**

        Activity

        Shaun Kalley created issue -
        Hide
        Stephen Colebourne added a comment -

        Patch applied, with some changes. Please retest. Thanks

        Show
        Stephen Colebourne added a comment - Patch applied, with some changes. Please retest. Thanks
        Henri Yandell made changes -
        Field Original Value New Value
        issue.field.bugzillaimportkey 35170 12342277
        Henri Yandell made changes -
        Assignee Jakarta Commons Developers Mailing List [ commons-dev@jakarta.apache.org ]
        Fix Version/s 2.2 [ 12311686 ]
        Project Commons [ 12310458 ] Commons Lang [ 12310481 ]
        Affects Version/s Nightly Builds [ 12311648 ]
        Component/s Lang [ 12311121 ]
        Key COM-2125 LANG-250
        Henri Yandell made changes -
        Affects Version/s Nightly Builds [ 12311718 ]
        Henri Yandell made changes -
        Fix Version/s 2.2 [ 12311702 ]
        Henri Yandell made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Mark Thomas made changes -
        Workflow jira [ 12371182 ] Default workflow, editable Closed status [ 12602381 ]
        Henri Yandell made changes -
        Affects Version/s Nightly Builds [ 12311718 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Shaun Kalley
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development