-
-
Save TomyLobo/a51be4b0b95c3db9bfdcefae35b62e24 to your computer and use it in GitHub Desktop.
Divran's string.Explode tests
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
local totable = string.ToTable | |
function Explode1(separator, str, withpattern) | |
if (separator == "") then return totable( str ) end | |
local ret = {} | |
local index,lastPosition = 1,1 | |
-- Escape all magic characters in separator | |
if not withpattern then separator = separator:gsub( "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1" ) end | |
if (#str > 10000) then | |
-- Find the parts | |
for startPosition,endPosition in str:gmatch( "()" .. separator.."()" ) do | |
ret[index] = str:sub(lastPosition, startPosition-1) | |
index = index + 1 | |
-- Keep track of the position | |
lastPosition = endPosition | |
end | |
else | |
-- Find the parts | |
for part,endPosition in str:gmatch( "(.-)" .. separator.."()" ) do | |
ret[index] = part | |
index = index + 1 | |
-- Keep track of the position | |
lastPosition = endPosition | |
end | |
end | |
-- Add last part by using the position we stored | |
ret[index] = str:sub(lastPosition) | |
return ret | |
end | |
function Explode2(separator, str, withpattern) | |
if (separator == "") then return totable( str ) end | |
local ret = {} | |
local index,lastPosition = 1,1 | |
-- Escape all magic characters in separator | |
if not withpattern then separator = separator:gsub( "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1" ) end | |
-- Find the parts | |
for startPosition,endPosition in str:gmatch( "()" .. separator.."()" ) do | |
ret[index] = str:sub(lastPosition, startPosition-1) | |
index = index + 1 | |
-- Keep track of the position | |
lastPosition = endPosition | |
end | |
-- Add last part by using the position we stored | |
ret[index] = str:sub(lastPosition) | |
return ret | |
end | |
function Explode3(separator, str, withpattern) | |
if (separator == "") then return totable( str ) end | |
local ret = {} | |
local index,lastPosition = 1,1 | |
-- Escape all magic characters in separator | |
if not withpattern then separator = separator:gsub( "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1" ) end | |
-- Find the parts | |
for part,endPosition in str:gmatch( "(.-)" .. separator.."()" ) do | |
ret[index] = part | |
index = index + 1 | |
-- Keep track of the position | |
lastPosition = endPosition | |
end | |
-- Add last part by using the position we stored | |
ret[index] = str:sub(lastPosition) | |
return ret | |
end | |
-- I used these strings to test | |
-- "long" string: | |
local str = ("aaaaaaaaaaaaaaaaaaaaaaa"):rep(2000) | |
str = str:sub(1,math.floor(#str/2)) .. "b" .. str:sub(math.floor(#str/2)+1) | |
local sep = "b" | |
-- "short" string: | |
local str = ("aab"):rep(100) -- test 1 | |
local str = ("aab"):rep(20000) -- test 2 | |
local str = ("aab"):rep(40000) -- test 3 | |
--[[ Test results: | |
Test 1: The "long string" which has massive parts and only one separator. The explode function is called only ONCE, and not in a loop. | |
------------------------------------- | |
long string char nr: 46001 | |
Explode1: 0.001220703125 | |
Explode2: 0.001220703125 | |
Explode3: 4.3232421875 | |
As you can see, #3 takes over 4 seconds to explode this string. | |
Test 2: The "short string" which consists of "aab" repeated 100 times, and the explode functions looped 10 000 times. | |
------------------------------------- | |
short string char nr: 300 | |
Explode1: 0.4273681640625 | |
Explode2: 0.581787109375 | |
Explode3: 0.375 | |
Test 3: The "short string" repeat has been increased to 20 000, and the loops have been commented out (so this is run ONCE). | |
------------------------------------- | |
short string char nr: 60000 | |
Explode1: 0.01123046875 | |
Explode2: 0.010498046875 | |
Explode3: 0.0057373046875 | |
Test 4: The "short string" repeat has been increased to 40 000, and the loops are still commented out. | |
------------------------------------- | |
short string char nr: 120000 | |
Explode1: 0.0240478515625 | |
Explode2: 0.022705078125 | |
Explode3: 0.013427734375 | |
]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment