我有一个超过200万个字符的字符串,我觉得我目前从模式中找到随机匹配的方式并不快. local function getRandomMatch(string, pattern) local occurenceCount = select(2, string.gsub(string, pattern, "")) local index,
local function getRandomMatch(string, pattern) local occurenceCount = select(2, string.gsub(string, pattern, "")) local index, randomIndex = 0, math.random(1, occurenceCount) for match in string:gmatch(pattern) do index = index + 1 if index == randomIndex then return match end end end
有没有办法可以更快?
local find, random, match = string.find, math.random, string.match local function getRandomMatch(string, pattern) local pos, random_pos = 0, 0 for cnt = 1, math.huge do pos = find(string, pattern, pos + 1) if not pos then return match(string, pattern, random_pos) elseif random(cnt) == 1 then random_pos = pos end end end for j = 1, 20 do print(getRandomMatch("1234", "%d%d")) end
更新:
快速和肮脏的解决方案:
(“Dirty”表示“匹配是随机的,但选择的概率不相等”)
local random, match = math.random, string.match local function getRandomMatchFastAndDirty(string, pattern) return match(string, pattern, random(#string)) or match(string, pattern) end