測(cè)試環(huán)境 win7 luaEditor V6.3
LuaEditor6.3
local Beg = os.time()
local str = "hello world"
for i = 1, 100000 do --10萬(wàn)
local res = str .. str
end
print(os.time() - Beg) --3s
local Beg = os.time()
local str = "hello world"
for i = 1, 100000 do --10萬(wàn)
local res = string.format("%s%s", str, str)
end
print(os.time() - Beg) --3s
在10W級(jí)別 沒(méi)什么差別 都是3秒
local Beg = os.time()
local str = "hello world"
for i = 1, 1000000 do --100萬(wàn)
local res = str .. str
end
print(os.time() - Beg) --29s
local Beg = os.time()
local str = "hello world"
for i = 1, 1000000 do --100萬(wàn)
local res = string.format("%s%s", str, str)
end
print(os.time() - Beg) --31s
在100W級(jí)別 鏈接符是29秒
string.format 是31秒
在百萬(wàn)級(jí)別替換字符串
--local str = "hello world"
local str = "hello world hello world hello world hello world hello world"
結(jié)果是
鏈接依然是29s
string.format 是32秒
結(jié)論..連接符比string.format更有效率
建議在代碼核心部分 使用連接符
當(dāng)然10萬(wàn)級(jí)別可以不用在意
百萬(wàn)級(jí)別需要注意
參考文章: http://blog.csdn.net/q277055799/article/details/11114395
具體原理該文章都有說(shuō)明 就不做論述了