forked from Mirrors/openclonk
Remove FindSubstring as it's really really slow
Use RegexSearch() instead, which is 100 times faster for large strings (see benchmark results below). Example benchmark: global func TestFindSubstring(int iterations) { var result; for (var i = 0; i < iterations; i++) { result = FindSubstring(hamlet, "and"); } return result; } global func TestRegexSearch(int iterations) { var result; for (var i = 0; i < iterations; i++) { result = RegexSearch(hamlet, "and"); } return result; } global func RunBenchmark(int iterations) { StartScriptProfiler(); Log("FindSubstring: %d iterations", iterations); var substr = TestFindSubstring(iterations); Log("RegexSearch: %d iterations", iterations); var regex = TestRegexSearch(iterations); StopScriptProfiler(); if (!DeepEqual(regex, substr)) Log("Results differ: %v vs %v", substr, regex); } Results: FindSubstring: 100 iterations RegexSearch: 100 iterations Profiler statistics: ============================== 48903ms Global.FindSubstring 48903ms Global.TestFindSubstring 47979ms Global.TakeString 00504ms Global.TestRegexSearch 00016ms Global.PushBack ==============================console-destruction
parent
b614209593
commit
db12ed7c99
|
@ -21,20 +21,6 @@ global func TakeString(string str, int begin, int end)
|
|||
return reduced_str;
|
||||
}
|
||||
|
||||
// Returns a list of positions where the substring was found in the main string.
|
||||
global func FindSubstring(string main_str, string sub_str)
|
||||
{
|
||||
var positions = [];
|
||||
var sub_length = GetLength(sub_str);
|
||||
var main_length = GetLength(main_str);
|
||||
if (sub_length > main_length)
|
||||
return positions;
|
||||
for (var index = 0; index <= main_length - sub_length; index++)
|
||||
if (TakeString(main_str, index, index + sub_length) == sub_str)
|
||||
PushBack(positions, index);
|
||||
return positions;
|
||||
}
|
||||
|
||||
// Converts a char into a string.
|
||||
global func CharToString(int char)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue