lua string.concat和..性能對比

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
  1. .. 的測試
    1. 一次性拼接,性能沒有什麼問題
    2. 嵌套循環使用效率奇低,原因是產生了大量的臨時字符,cpu的開銷大
  2. table.concat 的使用
    1. 嵌套的字符串拼接,concat的性能優秀:原因是lua底層使用了memcpy把所有的內容進行copy
    2. 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 */

性能分析文章: Does Lua optimize the “..” operator?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章