LUA字符串分割方法性能比较

来源:互联网 发布:24u网络机柜 编辑:程序博客网 时间:2024/05/18 09:09
function lua_string_split(s, p)
local rt = {}
string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
return rt
end












function split(str,splitor)
if(splitor==nil) then
splitor=","
end
local strArray={}
local strStart=1
--print(str)
--print(splitor);


local splitorLen = string.len(splitor)
--print(splitor);([^,]+)
local index=string.find(str,splitor,strStart)
if(index==nil) then
strArray[1]=str
return strArray
end
local i=1
while index do
strArray[i]=string.sub(str,strStart,index-1)
i=i+1
strStart=index+splitorLen
index = string.find(str,splitor,strStart)
end
strArray[i]=string.sub(str,strStart,string.len(str))
return strArray
end








function checklaj()
e = os.clock()
for i=100000,1,-1 do
local b = split("aa|bb|cc|ee|ff|gg|ii","|")
end
f = os.clock()
print(f-e)  --1.528293
end












function checktimer()
a = os.clock()
for i=100000,1,-1 do
local b = lua_string_split("aa|bb|cc|ee|ff|gg|ii","|")
end
c = os.clock()
print(c-a)  --1.528293
end




--checktimer();
checklaj();

checktimer();

这个运行时间 有时候代码挤一挤 速度就起来了

0.346
0.292

0 0