मैं ओरेकल बग डेटाबेस में एक आरएफई (वृद्धि के लिए अनुरोध) जमा करने पर विचार कर रहा हूं, जो स्ट्रिंग कॉन्सटेनेशन प्रदर्शन में काफी वृद्धि करना है। लेकिन इससे पहले कि मैं विशेषज्ञों की टिप्पणियों को सुनना चाहूंगा कि यह समझ में आता है या नहीं।क्या java.lang.String.concat में सुधार किया जा सकता है?
विचार इस तथ्य पर आधारित है कि मौजूदा स्ट्रिंग.कोनकैट (स्ट्रिंग) स्ट्रिंगबिल्डर की तुलना में 2 तारों पर दो गुना तेजी से काम करता है। समस्या यह है कि 3 या अधिक तारों को संयोजित करने की कोई विधि नहीं है। बाहरी तरीके ऐसा नहीं कर सकते क्योंकि String.concat एक पैकेज निजी कन्स्ट्रक्टर String(int offset, int count, char[] value)
का उपयोग करता है जो चार सरणी की प्रतिलिपि नहीं करता है लेकिन इसे सीधे उपयोग करता है। यह उच्च String.concat प्रदर्शन सुनिश्चित करता है। एक ही पैकेज में होने के नाते स्ट्रिंगबिल्डर अभी भी इस कन्स्ट्रक्टर का उपयोग नहीं कर सकता है क्योंकि तब स्ट्रिंग की चार सरणी संशोधनों के लिए उजागर की जाएगी।
मैं स्ट्रिंग
public static String concat(String s1, String s2)
public static String concat(String s1, String s2, String s3)
public static String concat(String s1, String s2, String s3, String s4)
public static String concat(String s1, String s2, String s3, String s4, String s5)
public static String concat(String s1, String... array)
नोट करने के लिए निम्न विधियों में जोड़ने के लिए सुझाव देते हैं: ओवरलोडिंग के इस प्रकार, EnumSet.of में प्रयोग किया जाता है दक्षता के लिए।
यह तरीकों में से एक के कार्यान्वयन है, दूसरों को, उसी तरह काम
public final class String {
private final char value[];
private final int count;
private final int offset;
String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
public static String concat(String s1, String s2, String s3) {
char buf[] = new char[s1.count + s2.count + s3.count];
System.arraycopy(s1.value, s1.offset, buf, 0, s1.count);
System.arraycopy(s2.value, s2.offset, buf, s1.count, s2.count);
System.arraycopy(s3.value, s3.offset, buf, s1.count + s2.count, s3.count);
return new String(0, buf.length, buf);
}
इसके अलावा के बाद इन तरीकों स्ट्रिंग, जावा के लिए जोड़ रहे
String s = s1 + s2 + s3;
के लिए कर सकेंगे कुशल वर्तमान अक्षम
केString s = String.concat(s1, s2, s3);
बजाय का निर्माण
String s = (new StringBuilder(String.valueOf(s1))).append(s2).append(s3).toString();
अद्यतन प्रदर्शन परीक्षण। मैंने इसे अपने नोटबुक इंटेल सेलेरॉन 925 पर चलाया, 3 स्ट्रिंग्स का संयोजन, मेरी स्ट्रिंग 2 कक्षा वास्तव में यह बताती है कि यह वास्तविक java.lang.String में कैसे होगा। स्ट्रिंग लम्बाई चुने जाते हैं ताकि स्ट्रिंगबिल्डर को सबसे प्रतिकूल परिस्थितियों में रखा जा सके, जब वह प्रत्येक परिशिष्ट पर अपनी आंतरिक बफर क्षमता का विस्तार करने की आवश्यकता होती है, जबकि कॉन्सट हमेशा एक बार [] को बनाता है।
public class String2 {
private final char value[];
private final int count;
private final int offset;
String2(String s) {
value = s.toCharArray();
offset = 0;
count = value.length;
}
String2(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
public static String2 concat(String2 s1, String2 s2, String2 s3) {
char buf[] = new char[s1.count + s2.count + s3.count];
System.arraycopy(s1.value, s1.offset, buf, 0, s1.count);
System.arraycopy(s2.value, s2.offset, buf, s1.count, s2.count);
System.arraycopy(s3.value, s3.offset, buf, s1.count + s2.count, s3.count);
return new String2(0, buf.length, buf);
}
public static void main(String[] args) {
String s1 = "1";
String s2 = "11111111111111111";
String s3 = "11111111111111111111111111111111111111111";
String2 s21 = new String2(s1);
String2 s22 = new String2(s2);
String2 s23 = new String2(s3);
long t0 = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String2 s = String2.concat(s21, s22, s23);
// String s = new StringBuilder(s1).append(s2).append(s3).toString();
}
System.out.println(System.currentTimeMillis() - t0);
}
}
1.000.000 पर पुनरावृत्तियों परिणाम हैं:
version 1 = ~200 ms
version 2 = ~400 ms
स्ट्रिंग बफर बहुत अधिक तेजी से है कि आप –