Advertisement
Guest User

string.Explode tests

a guest
Feb 23rd, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. local totable = string.ToTable
  2. function Explode1(separator, str, withpattern)
  3.     if (separator == "") then return totable( str ) end
  4.    
  5.     local ret = {}
  6.     local index,lastPosition = 1,1
  7.    
  8.     -- Escape all magic characters in separator
  9.     if not withpattern then separator = separator:gsub( "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1" ) end
  10.    
  11.     if (#str > 10000) then
  12.    
  13.         -- Find the parts
  14.         for startPosition,endPosition in str:gmatch( "()" .. separator.."()" ) do
  15.             ret[index] = str:sub(lastPosition, startPosition-1)
  16.             index = index + 1
  17.            
  18.             -- Keep track of the position
  19.             lastPosition = endPosition
  20.         end
  21.        
  22.     else
  23.    
  24.         -- Find the parts
  25.         for part,endPosition in str:gmatch( "(.-)" .. separator.."()" ) do
  26.             ret[index] = part
  27.             index = index + 1
  28.            
  29.             -- Keep track of the position
  30.             lastPosition = endPosition
  31.         end
  32.    
  33.     end
  34.    
  35.    
  36.     -- Add last part by using the position we stored
  37.     ret[index] = str:sub(lastPosition)
  38.     return ret
  39. end
  40.  
  41. function Explode2(separator, str, withpattern)
  42.     if (separator == "") then return totable( str ) end
  43.    
  44.     local ret = {}
  45.     local index,lastPosition = 1,1
  46.    
  47.     -- Escape all magic characters in separator
  48.     if not withpattern then separator = separator:gsub( "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1" ) end
  49.    
  50.     -- Find the parts
  51.     for startPosition,endPosition in str:gmatch( "()" .. separator.."()" ) do
  52.         ret[index] = str:sub(lastPosition, startPosition-1)
  53.         index = index + 1
  54.        
  55.         -- Keep track of the position
  56.         lastPosition = endPosition
  57.     end
  58.    
  59.     -- Add last part by using the position we stored
  60.     ret[index] = str:sub(lastPosition)
  61.     return ret
  62. end
  63.  
  64. function Explode3(separator, str, withpattern)
  65.     if (separator == "") then return totable( str ) end
  66.    
  67.     local ret = {}
  68.     local index,lastPosition = 1,1
  69.    
  70.     -- Escape all magic characters in separator
  71.     if not withpattern then separator = separator:gsub( "[%-%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1" ) end
  72.    
  73.     -- Find the parts
  74.     for part,endPosition in str:gmatch( "(.-)" .. separator.."()" ) do
  75.         ret[index] = part
  76.         index = index + 1
  77.        
  78.         -- Keep track of the position
  79.         lastPosition = endPosition
  80.     end
  81.    
  82.     -- Add last part by using the position we stored
  83.     ret[index] = str:sub(lastPosition)
  84.     return ret
  85. end
  86.  
  87. -- I used these strings to test
  88.  
  89. -- "long" string:
  90. local str = ("aaaaaaaaaaaaaaaaaaaaaaa"):rep(2000)
  91. str = str:sub(1,math.floor(#str/2)) .. "b" .. str:sub(math.floor(#str/2)+1)
  92. local sep = "b"
  93.  
  94. -- "short" string:
  95. local str = ("aab"):rep(100) -- test 1
  96. local str = ("aab"):rep(20000) -- test 2
  97. local str = ("aab"):rep(40000) -- test 3
  98.  
  99. --[[ Test results:
  100. 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.
  101. -------------------------------------
  102. long string char nr: 46001
  103. Explode1: 0.001220703125
  104. Explode2: 0.001220703125
  105. Explode3: 4.3232421875
  106. As you can see, #3 takes over 4 seconds to explode this string.
  107.  
  108. Test 2: The "short string" which consists of "aab" repeated 100 times, and the explode functions looped 10 000 times.
  109. -------------------------------------
  110. short string char nr: 300
  111. Explode1: 0.4273681640625
  112. Explode2: 0.581787109375
  113. Explode3: 0.375
  114.  
  115. Test 3: The "short string" repeat has been increased to 20 000, and the loops have been commented out (so this is run ONCE).
  116. -------------------------------------
  117. short string char nr: 60000
  118. Explode1: 0.01123046875
  119. Explode2: 0.010498046875
  120. Explode3: 0.0057373046875
  121.  
  122. Test 4: The "short string" repeat has been increased to 40 000, and the loops are still commented out.
  123. -------------------------------------
  124. short string char nr: 120000
  125. Explode1: 0.0240478515625
  126. Explode2: 0.022705078125
  127. Explode3: 0.013427734375
  128. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement