从lua中的一个非常长的字符串中获取随机模式匹配的最快方法是什么?

前端之家收集整理的这篇文章主要介绍了从lua中的一个非常长的字符串中获取随机模式匹配的最快方法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个超过200万个字符的字符串,我觉得我目前从模式中找到随机匹配的方式并不快.

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,pos + 1)
      if not pos then
         return match(string,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,random(#string)) or match(string,pattern)
end

猜你在找的Lua相关文章