तो, एडम राल्फ के पद को देखने के बाद, मुझे रेगेक्स समाधान से तेज़ होने का उनका समाधान संदेह था। बस सोचा कि मैं अपने परीक्षण के परिणाम साझा करूंगा क्योंकि मुझे लगता है कि यह तेज़ था। उप-तार निकाले (सीमांकक की संख्या द्वारा निर्धारित) की संख्या, और कुल स्ट्रिंग लंबाई:
खेल में वास्तव में दो कारकों (सिस्टम चर अनदेखी) कर रहे हैं। नीचे प्लॉट किया गया बहुत ही सरल परिदृश्य "ए" का उपयोग दो सफेद स्पेस वर्णों (टैब के बाद एक स्थान) द्वारा सीमित उप-स्ट्रिंग के रूप में करता है। यह निकाले गए उप-तारों की संख्या के प्रभाव को बढ़ाता है। मैं आगे बढ़ गया और मेरे ऑपरेटिंग सिस्टम के लिए निम्नलिखित सामान्य समीकरणों पर पहुंचने के लिए कुछ एकाधिक चर परीक्षण किया।
Regex()
टी = (28.33 * एसएसएल + 572) (एसएसएन/10^6)
स्प्लिट()। कहाँ()
टी = (6।23 * एसएसएल + 250) (एसएसएन/10^6)
जहां टी मिलीसेकंड में निष्पादन समय है, एसएसएल औसत उप-स्ट्रिंग लंबाई है, और एसएसएन स्ट्रिंग में सीमित उप-तारों की संख्या है।
इन समीकरणों के रूप में भी
टी = (28.33 * SL + 572 * एसएसएन)/10^6
और
टी = (6.23 * SL + 250 * एसएसएन) लिखा जा सकता है/10^6
जहां SL कुल स्ट्रिंग लंबाई (SL = SSL * एसएसएन)
निष्कर्ष है: स्प्लिट() कहाँ।() समाधान Regex() से तेज है। प्रमुख कारक उप-तारों की संख्या है, जबकि स्ट्रिंग की लंबाई एक मामूली भूमिका निभाती है। संबंधित गुणांक के लिए प्रदर्शन लाभ लगभग 2x और 5x हैं। (आवश्यकता से शायद रास्ता अधिक सामग्री है, लेकिन यह बहु चर डेटा प्राप्त करने के लिए सेट अप मैं बारे में बात की है)
यहाँ मेरी परीक्षण कोड है
using System;
using System.Linq;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
public enum TestMethods {regex, split};
[STAThread]
static void Main(string[] args)
{
//Compare TestMethod execution times and output result information
//to the console at runtime and to the clipboard at program finish (so that data is ready to paste into analysis environment)
#region Config_Variables
//Choose test method from TestMethods enumerator (regex or split)
TestMethods TestMethod = TestMethods.split;
//Configure RepetitionString
String RepetitionString = string.Join(" \t", Enumerable.Repeat("A",100));
//Configure initial and maximum count of string repetitions (final count may not equal max)
int RepCountInitial = 100;int RepCountMax = 1000 * 100;
//Step increment to next RepCount (calculated as 20% increase from current value)
Func<int, int> Step = x => (int)Math.Round(x/5.0, 0);
//Execution count used to determine average speed (calculated to adjust down to 1 execution at long execution times)
Func<double, int> ExecutionCount = x => (int)(1 + Math.Round(500.0/(x + 1), 0));
#endregion
#region NonConfig_Variables
string s;
string Results = "";
string ResultInfo;
double ResultTime = 1;
#endregion
for (int RepCount = RepCountInitial; RepCount < RepCountMax; RepCount += Step(RepCount))
{
s = string.Join("", Enumerable.Repeat(RepetitionString, RepCount));
ResultTime = Test(s, ExecutionCount(ResultTime), TestMethod);
ResultInfo = ResultTime.ToString() + "\t" + RepCount.ToString() + "\t" + ExecutionCount(ResultTime).ToString() + "\t" + TestMethod.ToString();
Console.WriteLine(ResultInfo);
Results += ResultInfo + "\r\n";
}
Clipboard.SetText(Results);
}
public static double Test(string s, int iMax, TestMethods Method)
{
switch (Method)
{
case TestMethods.regex:
return Math.Round(RegexRunTime(s, iMax),2);
case TestMethods.split:
return Math.Round(SplitRunTime(s, iMax),2);
default:
return -1;
}
}
private static double RegexRunTime(string s, int iMax)
{
Stopwatch sw = new Stopwatch();
sw.Restart();
for (int i = 0; i < iMax; i++)
{
System.Collections.Generic.IEnumerable<string> ens = Regex.Split(s, @"\s+");
}
sw.Stop();
return Math.Round(sw.ElapsedMilliseconds/(double)iMax, 2);
}
private static double SplitRunTime(string s,int iMax)
{
Stopwatch sw = new Stopwatch();
sw.Restart();
for (int i = 0; i < iMax; i++)
{
System.Collections.Generic.IEnumerable<string> ens = s.Split().Where(x => x != string.Empty);
}
sw.Stop();
return Math.Round(sw.ElapsedMilliseconds/(double)iMax, 2);
}
}
}
अतिरिक्त व्हाइटस्पेस को हटाने के लिए स्प्लिटस्ट्रिंगऑप्शन के साथ भी संभावित डुप्लिकेट देखें। http://stackoverflow.com/questions/6111298/best-way-to- निर्दिष्ट- whitespace-in-a-string-split-operation – goodeye