-
-
Save polatengin/20456c4700311bd30f3d19c865a85173 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
C# String.IndexOf, String.Contains, Regex.IsMatch(), Regex.IsMatch() Compiled performans karşılaştırma |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void InvokeTests() | |
{ | |
TestAndPrint(() => { StringIndexOfTest("dolor"); }, nameof(StringIndexOfTest) + "-NoMatch"); | |
TestAndPrint(() => { StringIndexOfTest("engin"); }, nameof(StringIndexOfTest) + "-Match"); | |
TestAndPrint(() => { StringContainsTest("dolor"); }, nameof(StringContainsTest) + "-NoMatch"); | |
TestAndPrint(() => { StringContainsTest("engin"); }, nameof(StringContainsTest) + "-Match"); | |
TestAndPrint(() => { RegexIsMatchTest("dolor"); }, nameof(RegexIsMatchTest) + "-NoMatch"); | |
TestAndPrint(() => { RegexIsMatchTest("engin"); }, nameof(RegexIsMatchTest) + "-Match"); | |
TestAndPrint(() => { RegexIsMatchCompiledTest("dolor"); }, nameof(RegexIsMatchCompiledTest) + "-NoMatch"); | |
TestAndPrint(() => { RegexIsMatchCompiledTest("engin"); }, nameof(RegexIsMatchCompiledTest) + "-Match"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Test başlıyor!"); | |
var scenarios = new TestScenarios(); | |
scenarios.InvokeTests(); | |
Console.WriteLine("Test tamamlandı!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void RegexIsMatchTest(string comparedTo) | |
{ | |
for(var iLoop = 0; iLoop < this.TestIterationCount; iLoop++) | |
{ | |
Regex.IsMatch(TestString, comparedTo); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void RegexIsMatchCompiledTest(string comparedTo) | |
{ | |
for(var iLoop = 0; iLoop < this.TestIterationCount; iLoop++) | |
{ | |
Regex.IsMatch(TestString, comparedTo, RegexOptions.Compiled); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Pure] | |
public bool Contains( string value ) { | |
return ( IndexOf(value, StringComparison.Ordinal) >=0 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void StringContainsTest(string comparedTo) | |
{ | |
for(var iLoop = 0; iLoop < this.TestIterationCount; iLoop++) | |
{ | |
TestString.Contains(comparedTo); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void StringIndexOfTest(string comparedTo) | |
{ | |
for(var iLoop = 0; iLoop < this.TestIterationCount; iLoop++) | |
{ | |
TestString.IndexOf(comparedTo); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void TestAndPrint(Action Test, string testName) | |
{ | |
var watch = new Stopwatch(); | |
GC.Collect(2, GCCollectionMode.Forced); | |
GC.WaitForFullGCComplete(); | |
watch.Restart(); | |
Test(); | |
Console.WriteLine($"{testName} testi {watch.ElapsedMilliseconds / 10D} ms. sürdü"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int TestIterationCount = 10_000_000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string TestString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur scelerisque libero eros, et efficitur mi laoreet ac"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment