Improve string.end/start with function performance#2406
Open
wrefgtzweve wants to merge 4 commits intoFacepunch:masterfrom
Open
Improve string.end/start with function performance#2406wrefgtzweve wants to merge 4 commits intoFacepunch:masterfrom
wrefgtzweve wants to merge 4 commits intoFacepunch:masterfrom
Conversation
Collaborator
|
Uh, I am a bit confused. Your own tests show 140% slowdown that the proposed changes would create? (with JIT on, the default) In your test code test 1 is your new implementation, and test 2 is old implementation. With my own testing, it seems to be extremely inconsistent, and between +-1% faster/slower for string.EndsWith and ~40% slower for Replacing the input strings with your Code used for testingfunction string.StartsWithOld( str, start )
return string.sub( str, 1, string.len( start ) ) == start
end
function string.EndsWithOld( str, endStr )
return endStr == "" or string.sub( str, -string.len( endStr ) ) == endStr
end
function string.StartsWithNew( str, start )
local n = #start
return n == 0 or string.sub(str, 1, n) == start
end
function string.EndsWithNew( str, endStr )
local n = #endStr
return n == 0 or string.sub(str, -n) == endStr
end
local count = 10000000
local strings = {
"testing", "te",
"testing", "da",
"testing", "",
"test", "",
"fuck", "ck",
"dadaodjnaiduahioduhaidouhadg", "da",
}
local StartTime = SysTime()
for i = 1, count do
local id1 = ( ( i * 2 ) - 1 ) % (#strings)
string.StartsWithOld( strings[ id1 ], strings[ id1 + 1 ] )
//string.ExplodeOld( " ", "test test test" )
end
local OldTotal = SysTime() - StartTime
print("Old: " .. OldTotal .. " seconds.")
local StartTime = SysTime()
for i = 1, count do
local id1 = ( ( i * 2 ) - 1 ) % (#strings)
string.StartsWithNew( strings[ id1 ], strings[ id1 + 1 ] )
//string.Explode( " ", "test test test" )
end
local NewTotal = SysTime()- StartTime
print("New: " .. NewTotal .. " seconds. ")
if ( NewTotal > OldTotal ) then
MsgN("New is " .. math.Round( ( NewTotal - OldTotal ) / OldTotal * 100, 3 ), "% slower than old" )
else
MsgN("New is " .. math.Round( ( OldTotal - NewTotal ) / NewTotal * 100, 3 ), "% faster than old" )
end
end ) |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Improves the speed of
string.StartsWithandstring.EndsWithTest lib https://github.com/CFC-Servers/gm_nitrous/blob/main/lua/nitrous/utils/testlib.lua