नीचे, अपने प्रश्न और कोड के अपने विश्लेषण (टिप्पणी पढ़ने)
using (StreamReader srSegmentData = new StreamReader(fileNamePath))
{
string strSegmentData = "";
string line = srSegmentData.ReadToEnd(); // Why are you reading this till the end if it is such a long string?
int startPos = 0;
ArrayList alSegments = new ArrayList(); // Better choice would be to use List<string>
while (startPos < line.Length && (line.Length - startPos) >= segmentSize)
{
strSegmentData = strSegmentData + line.Substring(startPos, segmentSize) + Environment.NewLine; // Seem like you are inserting linebreaks at specified interval in your original string. Is that what you want?
alSegments.Add(line.Substring(startPos, segmentSize) + Environment.NewLine); // Why are you recalculating the Substring? Why are you appending the newline if the aim is to just "split"
startPos = startPos + segmentSize;
}
}
धारणा सभी तरह बनाना है नीचे कोड मैं लंबी स्ट्रिंग विभाजित करने के लिए सिफारिश करेंगे है। नमूना में आप जो कर रहे हैं वह करने का यह एक साफ तरीका है। आप इसे अनुकूलित कर सकते हैं, लेकिन यह सुनिश्चित नहीं कर सकते कि आप कितनी तेजी से खोज रहे हैं।
static void Main(string[] args) {
string fileNamePath = "ConsoleApplication1.pdb";
var segmentSize = 32;
var op = ReadSplit(fileNamePath, segmentSize);
var joinedSTring = string.Join(Environment.NewLine, op);
}
static List<string> ReadSplit(string filePath, int segmentSize) {
var splitOutput = new List<string>();
using (var file = new StreamReader(filePath, Encoding.UTF8, true, 8 * 1024)) {
char []buffer = new char[segmentSize];
while (!file.EndOfStream) {
int n = file.ReadBlock(buffer, 0, segmentSize);
splitOutput.Add(new string(buffer, 0, n));
}
}
return splitOutput;
}
मैंने अपने संस्करण पर कोई प्रदर्शन परीक्षण नहीं किया है, लेकिन मेरा अनुमान है कि यह आपके संस्करण से तेज़ है।
इसके अलावा, मुझे यकीन नहीं है कि आप आउटपुट का उपभोग करने की योजना कैसे बनाते हैं, लेकिन I/O करते समय एक अच्छा अनुकूलन एसिंक कॉल का उपयोग करना है।और (पठनीयता और जटिलता की कीमत पर) एक अच्छा अनुकूलन जब बड़े से निपटने string
char[]
ध्यान दें कि
- आप फ़ाइल
- पढ़ते समय चरित्र एन्कोडिंग मुद्दों से निपटने के लिए हो सकता है के साथ रहना है आप पहले से ही स्मृति और फ़ाइल पढ़ने में लंबी स्ट्रिंग सिर्फ डेमो में शामिल किया गया था, तो आप
StreamReader
वर्ग के बजाय StringReader
वर्ग का उपयोग करना चाहिए
'String.S plit' एक विकल्प हो सकता है –
इससे मदद मिल सकती है: http://stackoverflow.com/questions/568968/does-any-one-now-of-a-faster-method-to-do-string-split – MusicLovingIndianGirl
हम डॉन ' स्प्लिट का उपयोग करने के लिए कोई विशिष्ट चरित्र नहीं है, बस आकार (वर्णों की संख्या) के आधार पर स्ट्रिंग को अलग करना होगा –