DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=35170>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=35170

           Summary: [PATCH] [lang] Performance boost for RandomStringUtils
           Product: Commons
           Version: Nightly Builds
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Lang
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: [EMAIL PROTECTED]


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);
     }
 
     /**

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to