print("-------------------------------------test1-------------------------------------")
--[[
测试用例:测试简单字符拼接
--]]
local a = "hello"
local b = "lua"
local c = "world"
local startTime = os.clock()
local str = nil
for i=1, 100000 do
str = a .. "," .. b .. "," .. c .. "," .. os.clock()
end
local endTime = os.clock()
print("case1 time used:", endTime - startTime)
local startTime = os.clock()
local str = nil
for i=1, 100000 do
-- 创建table有性能开销
local tb = {}
table.insert(tb, a)
table.insert(tb, b)
table.insert(tb, c)
table.insert(tb, os.clock())
str = table.concat(tb,",")
end
local endTime = os.clock()
print("case2 time used:", endTime - startTime)
print("-------------------------------------test2-------------------------------------")
--[[
测试用例:测试长字符串
--]]
local a = "hellohellohellohellohellohellohellohellohello1"
local b = "hellohellohellohellohellohellohellohellohello2"
local c = "hellohellohellohellohellohellohellohellohello3"
local str = nil
local startTime = os.clock()
for i=1, 100000 do
str = a .. "," .. b .. "," .. c .. "," .. os.clock()
end
local endTime = os.clock()
print("case1 time used:", endTime - startTime)
local startTime = os.clock()
local str = nil
for i=1, 100000 do
local tb = {}
table.insert(tb, a)
table.insert(tb, b)
table.insert(tb, c)
table.insert(tb, os.clock())
local str = table.concat(tb,",")
end
local endTime = os.clock()
print("case2 time used:", endTime - startTime)
print("-------------------------------------test3-------------------------------------")
--[[
测试用例:测试嵌套字符串
--]]
function getTable2RecordString1(tbRecords)
local str = ""
for _,value in ipairs(tbRecords) do
str = str .. value.dId .. "," .. value.strName .. "," .. value.dTest1 .. "," .. value.dTest2 .. "," .. value.dTest3 .. "," .. value.dTest4 .. ";"
end
return str
end
function getTable2RecordString2(tbRecords)
local str = ""
local tbResult = {}
for _,value in ipairs(tbRecords) do
local tbItem = {}
table.insert(tbItem, value.dId)
table.insert(tbItem, value.strName)
table.insert(tbItem, value.dTest1)
table.insert(tbItem, value.dTest2)
table.insert(tbItem, value.dTest3)
table.insert(tbItem, value.dTest4)
table.insert(tbResult, table.concat(tbItem, ","))
end
str = table.concat(tbResult, ",")
return str
end
local tbRecords = {}
for i=1,10000 do
local tb = {}
tb.dId = math.random(10000000,999999999)
tb.strName = "name" .. math.random(10000000,999999999)
tb.dTest1 = math.random(10000000,999999999)
tb.dTest2 = math.random(10000000,999999999)
tb.dTest3 = math.random(10000000,999999999)
tb.dTest4 = math.random(10000000,999999999)
table.insert(tbRecords, tb)
end
local startTime = os.clock()
local str1 = getTable2RecordString1(tbRecords)
local endTime = os.clock()
print("case1 time used:", endTime - startTime)
local startTime = os.clock()
local str2 = getTable2RecordString2(tbRecords)
local endTime = os.clock()
print("case2 time used:", endTime - startTime)
运行结果举例:
-------------------------------------test1-------------------------------------
case1 time used: 0.054
case2 time used: 0.146
-------------------------------------test2-------------------------------------
case1 time used: 0.066
case2 time used: 0.165
-------------------------------------test3-------------------------------------
case1 time used: 1.444
case2 time used: 0.095
- 小结:字符串拼接够多,特别是循环里使用的时候,最好使用table.concat
- .. 的测试
- 一次性拼接,性能没有什么问题
- 嵌套循环使用效率奇低,原因是产生了大量的临时字符,cpu的开销大
- table.concat 的使用
- 嵌套的字符串拼接,concat的性能优秀:原因是lua底层使用了memcpy把所有的内容进行copy
-
do { /* concat all strings */ size_t l = tsvalue(top-i)->len; memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); tl += l; } while (--i > 0); setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); total -= n-1; /* got 'n' strings to create 1 new */ L->top -= n-1; /* popped 'n' strings and pushed one */