commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 35170] New: - [PATCH] [lang] Performance boost for RandomStringUtils
Date Thu, 02 Jun 2005 02:55:11 GMT
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: shaun_kalley@telus.net


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: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message