Lua脚本优化技巧:别让你的服务器卡成PPT
Lua脚本优化技巧:别让服务器卡成PPT—— 你的脚本可能在偷偷"吃"服务器性能 ——
很多服主写Lua脚本只管功能不管性能,结果——
玩家一多,服务器卡得比PPT还流畅。:Q
技巧1:少用全局变量
-- 不推荐:
function addCount()
count = count + 1-- 全局查找,慢
end
-- 推荐:
local count = 0
local function addCount()
count = count + 1-- 局部upvalue,快!
end
技巧2:字符串拼接用table.concat
-- 慢!每次拼接都创建新字符串
local s = ""
for i = 1, 1000 do
s = s .. "line" .. i .. "\n"
end
-- 快!
local t = {}
for i = 1, 1000 do
t = "line" .. i .. "\n"
end
local s = table.concat(t)
[*] 原则1:能用局部就用局部
[*] 原则2:减少GC压力
[*] 原则3:避免每帧都做复杂计算
好的脚本就像好的代码——别人看不懂,但跑得贼快 :D
想学习更多优化技巧?:http://bbs.vvm2.cc/?fromuser=blue
页:
[1]