मैं इस क्षेत्र में एक विशेषज्ञ होने का दावा नहीं करता हूं, हालांकि मुझे कम से कम कुछ हद तक प्रोफाइल करने के लिए मजबूर होना पड़ा (पूरी तरह से जानना कि मेरा कल्पित परिदृश्य आपके से अलग होगा) और यहां मैं यही हूं के साथ आया था:
ऐसा लगता है, कम से कम मेरे लिए, EndsWith
LastIndexOf
लगातार दूसरे में आने के साथ ले लेता है, कुछ समय कर रहे हैं:
SubString: 00:00:00.0191877
Contains: 00:00:00.0201980
CompareInfo: 00:00:00.0255181
EndsWith: 00:00:00.0120296
LastIndexOf: 00:00:00.0133181
ये 100,000 तार प्रसंस्करण जहां वांछित प्रत्यय दिखाई दिया से बटोरा गया में सभी स्ट्रिंग्स और इसलिए मेरे लिए बस जॉन के जवाब को आकर्षित करता है (जहां लाभ गति और वर्णनात्मकता दोनों है)। और उपयोग किया गया कोड इन परिणामों में आने के लिए:
class Program
{
class Profiler
{
private Stopwatch Stopwatch = new Stopwatch();
public TimeSpan Elapsed { get { return Stopwatch.Elapsed; } }
public void Start()
{
Reset();
Stopwatch.Start();
}
public void Stop()
{
Stopwatch.Stop();
}
public void Reset()
{
Stopwatch.Reset();
}
}
static string suffix = "_sfx";
static Profiler profiler = new Profiler();
static List<string> input = new List<string>();
static List<string> output = new List<string>();
static void Main(string[] args)
{
GenerateSuffixedStrings();
FindStringsWithSuffix_UsingSubString(input, suffix);
Console.WriteLine("SubString: {0}", profiler.Elapsed);
FindStringsWithSuffix_UsingContains(input, suffix);
Console.WriteLine("Contains: {0}", profiler.Elapsed);
FindStringsWithSuffix_UsingCompareInfo(input, suffix);
Console.WriteLine("CompareInfo: {0}", profiler.Elapsed);
FindStringsWithSuffix_UsingEndsWith(input, suffix);
Console.WriteLine("EndsWith: {0}", profiler.Elapsed);
FindStringsWithSuffix_UsingLastIndexOf(input, suffix);
Console.WriteLine("LastIndexOf: {0}", profiler.Elapsed);
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
static void GenerateSuffixedStrings()
{
for (var i = 0; i < 100000; i++)
{
input.Add(Guid.NewGuid().ToString() + suffix);
}
}
static void FindStringsWithSuffix_UsingSubString(IEnumerable<string> strings, string suffix)
{
output.Clear();
profiler.Start();
foreach (var s in strings)
{
if(s.Substring(s.Length - 4) == suffix)
output.Add(s);
}
profiler.Stop();
}
static void FindStringsWithSuffix_UsingContains(IEnumerable<string> strings, string suffix)
{
output.Clear();
profiler.Start();
foreach (var s in strings)
{
if (s.Contains(suffix))
output.Add(s);
}
profiler.Stop();
}
static void FindStringsWithSuffix_UsingCompareInfo(IEnumerable<string> strings, string suffix)
{
var ci = CompareInfo.GetCompareInfo("en-GB");
output.Clear();
profiler.Start();
foreach (var s in strings)
{
if (ci.IsSuffix(s, suffix))
output.Add(s);
}
profiler.Stop();
}
static void FindStringsWithSuffix_UsingEndsWith(IEnumerable<string> strings, string suffix)
{
output.Clear();
profiler.Start();
foreach (var s in strings)
{
if (s.EndsWith(suffix))
output.Add(s);
}
profiler.Stop();
}
static void FindStringsWithSuffix_UsingLastIndexOf(IEnumerable<string> strings, string suffix)
{
output.Clear();
profiler.Start();
foreach (var s in strings)
{
if (s.LastIndexOf(suffix) == s.Length - 4)
output.Add(s);
}
profiler.Stop();
}
}
संपादित करें:
के रूप में टिप्पणी की, मैं केवल के साथ फिर से इस कोशिश की एक प्रत्यय आवेदन किया है और हो रही तार से कुछ इन परिणामों हैं:
SubString: 00:00:00.0079731
Contains: 00:00:00.0243696
CompareInfo: 00:00:00.0334056
EndsWith: 00:00:00.0196668
LastIndexOf: 00:00:00.0229599
स्ट्रिंग जनरेटर विधि इस प्रकार है अद्यतन किया गया था, तार उत्पादन करने के लिए:
static void GenerateSuffixedStrings()
{
var nxt = false;
var rnd = new Random();
for (var i = 0; i < 100000; i++)
{
input.Add(Guid.NewGuid().ToString() +
(rnd.Next(0, 2) == 0 ? suffix : string.Empty));
}
}
इसके अलावा, इस प्रवृत्ति को जारी रहा तो स्ट्रिंग में से कोई भी एक प्रत्यय है:
SubString: 00:00:00.0055584
Contains: 00:00:00.0187089
CompareInfo: 00:00:00.0228983
EndsWith: 00:00:00.0114227
LastIndexOf: 00:00:00.0199328
हालांकि, इस अंतर को फिर से छोटा कर देता आदानों की एक चौथाई एक प्रत्यय (पहली तिमाही बताए, तो randomise
को छँटाई कवरेज):
SubString: 00:00:00.0302997
Contains: 00:00:00.0305685
CompareInfo: 00:00:00.0306335
EndsWith: 00:00:00.0351229
LastIndexOf: 00:00:00.0322899
निष्कर्ष?आईएमओ, और जॉन के साथ सहमत, EndsWith
जाने का रास्ता लगता है (वैसे भी इस सीमित परीक्षण के आधार पर)।
आगे संपादित करें:
जॉन की जिज्ञासा का इलाज करने के लिए मैं के साथ या बिना Ordinal
स्ट्रिंग तुलना, EndsWith
पर कुछ और परीक्षण भाग गया ...
उनमें से एक चौथाई के साथ 100,000 तार पर प्रत्यय:
EndsWith: 00:00:00.0795617
OrdinalEndsWith: 00:00:00.0240631
उनमें से एक चौथाई प्रत्यय के साथ 1,000,000 तार पर:
EndsWith: 00:00:00.5460591
OrdinalEndsWith: 00:00:00.2807860
उनमें से एक चौथाई प्रत्यय के साथ 10,000,000 तार पर:
EndsWith: 00:00:07.5889581
OrdinalEndsWith: 00:00:03.3248628
ध्यान दें कि मैं केवल अंतिम परीक्षण एक बार तार पैदा करने के रूप में साबित कर दिया इस लैपटॉप एक प्रतिस्थापन की जरूरत है भाग गया
.NET क्या कर रहा है, इस पर कोई टिप्पणी नहीं है, लेकिन यदि आपको कई प्रत्यय के लिए एक ही स्ट्रिंग का परीक्षण करने की आवश्यकता है, तो प्रत्यय पेड़ का निर्माण करने की सलाह दी जा सकती है: http://en.wikipedia.org/wiki/Suffix_tree – mquander