当前位置: 首页 > news >正文

最准做特马网站江苏短视频seo搜索

最准做特马网站,江苏短视频seo搜索,餐饮加盟网站模板,wordpress rockgroup常见的Lua优化小技巧 Lua常见优化点:1. 尽量使用局部变量2. table的相关减少对表的访问for循环预分配表空间元表 3. string的相关4. 避免运行时加载编译5. 尽量避免频繁创建临时对象闭包表 Lua常见优化点: 1. 尽量使用局部变量 尽量将变量局部化&#x…

常见的Lua优化小技巧

  • Lua常见优化点:
    • 1. 尽量使用局部变量
    • 2. table的相关
      • 减少对表的访问
      • for循环
      • 预分配表空间
      • 元表
    • 3. string的相关
    • 4. 避免运行时加载编译
    • 5. 尽量避免频繁创建临时对象
      • 闭包

Lua常见优化点:

1. 尽量使用局部变量

尽量将变量局部化,尤其是频繁使用的变量,是Lua最重要的优化方式

-- 使用全局变量
function sumGlobal(n)local sum = 0for i = 1, n dosum = sum + GLOBAL_VALUEendreturn sum
end-- 使用局部变量
function sumLocal(n)local sum = 0local local_value = GLOBAL_VALUEfor i = 1, n dosum = sum + local_valueendreturn sum
endGLOBAL_VALUE = 1local n = 1000000
local start_time = os.clock()
sumGlobal(n)
print("sumGlobal:", os.clock() - start_time)start_time = os.clock()
sumLocal(n)
print("sumLocal:", os.clock() - start_time)-- sumGlobal: 0.020
-- sumLocal: 0.010

2. table的相关

减少对表的访问

表访问也有一定的开销,可以将常用的表元素存储在局部变量中

local t = {x = 1, y = 2, z = 3}-- 直接访问表
function accessTableDirectly(n)local sum = 0for i = 1, n dosum = sum + t.x + t.y + t.zendreturn sum
end-- 缓存表元素
function accessTableLocally(n)local sum = 0local x, y, z = t.x, t.y, t.zfor i = 1, n dosum = sum + x + y + zendreturn sum
endn = 1000000
start_time = os.clock()
accessTableDirectly(n)
print("accessTableDirectly:", os.clock() - start_time)start_time = os.clock()
accessTableLocally(n)
print("accessTableLocally:", os.clock() - start_time)-- accessTableDirectly: 0.030
-- accessTableLocally: 0.015

for循环

数值 for 循环在 Lua 中比泛型 for 循环更快

local t = {}
for i = 1, 1000000 dot[i] = i
end-- 泛型 for 循环
function genericForLoop(n)local sum = 0for _, v in ipairs(t) dosum = sum + vendreturn sum
end-- 数值 for 循环
function numericForLoop(n)local sum = 0for i = 1, n dosum = sum + t[i]endreturn sum
endn = 1000000
start_time = os.clock()
genericForLoop(n)
print("genericForLoop:", os.clock() - start_time)start_time = os.clock()
numericForLoop(n)
print("numericForLoop:", os.clock() - start_time)-- genericForLoop: 0.060
-- numericForLoop: 0.030

预分配表空间

在创建大型表时,预先分配表的大小可以提高性能

-- 动态增加表大小
function dynamicTable(n)local t = {}for i = 1, n dot[i] = iendreturn t
end-- 预分配表大小
function preallocatedTable(n)local t = {}for i = 1, n dot[i] = iendreturn t
endn = 1000000
start_time = os.clock()
dynamicTable(n)
print("dynamicTable:", os.clock() - start_time)start_time = os.clock()
preallocatedTable(n)
print("preallocatedTable:", os.clock() - start_time)-- dynamicTable: 0.050
-- preallocatedTable: 0.040

元表

  1. 频繁设置元表:
    每次对象创建时都设置元表,导致大量内存分配和元表初始化操作。
    因为每次都重新设置元表,导致性能开销最大。
  2. 预定义元表:
    预先定义好元表,并在对象创建时一次性设置,减少了频繁设置元表的开销。
    相比频繁设置元表,性能显著提升。
  3. 缓存元方法:
    通过将元方法缓存到局部变量中,避免了每次访问属性时的元表查找。
    进一步减少了运行时的查找和调用开销,性能最佳。
-- 频繁设置元表
local start_time = os.clock()
local function createObject()local obj = {}setmetatable(obj, {__index = function(t, k)return "value"end,__newindex = function(t, k, v)rawset(t, k, v)end,})return obj
endfor i = 1, 1000000 dolocal obj = createObject()local value = obj.some_key
endprint("Time taken with frequent setmetatable:", os.clock() - start_time)
---------------------------------------------------------------------------- 预定义元表
local start_time = os.clock()
local mt = {__index = function(t, k)return "value"end,__newindex = function(t, k, v)rawset(t, k, v)end,
}local function createObject()local obj = {}setmetatable(obj, mt)return obj
endfor i = 1, 1000000 dolocal obj = createObject()local value = obj.some_key
endprint("Time taken with predefined metatable:", os.clock() - start_time)
---------------------------------------------------------------------------- 缓存元方法
local start_time = os.clock()local mt = {__index = function(t, k)return "value"end,__newindex = function(t, k, v)rawset(t, k, v)end,
}local obj = setmetatable({}, mt)
local __index = mt.__index
for i = 1, 1000000 dolocal value = __index(obj, "some_key")
endprint("Time taken with cached metatable method:", os.clock() - start_time)-- Time taken with frequent setmetatable: 1.5 seconds
-- Time taken with predefined metatable: 0.3 seconds
-- Time taken with cached metatable method: 0.1 seconds

3. string的相关

  1. 对于少量字符串连接,… 操作符非常方便。然而,当需要连接大量字符串时,使用 … 操作符的性能会显著下降。因为每次使用 … 操作符都会创建一个新的字符串,涉及大量的内存分配和数据复制操作。
  2. table.concat 函数用于连接表中的字符串,性能优于 … 操作符,特别是在连接大量字符串时。
-- 使用字符串连接操作符
-- 每次循环迭代都会创建一个新的字符串,并将结果赋值给 result。
-- 由于每次都要分配新的内存并复制已有的字符串内容,导致性能开销较大。
function concatOperator(n)local str = ""for i = 1, n dostr = str .. iendreturn str
end-- 使用 table.concat
-- 将所有字符串存储在一个表中,然后使用 table.concat 一次性连接所有字符串。
-- 这种方式只需要一次内存分配和数据复制操作,性能开销较小。
function concatTable(n)local t = {}for i = 1, n dot[#t + 1] = iendreturn table.concat(t)
endn = 10000
start_time = os.clock()
concatOperator(n)
print("concatOperator:", os.clock() - start_time)start_time = os.clock()
concatTable(n)
print("concatTable:", os.clock() - start_time)-- concatOperator: 2.500
-- concatTable: 0.050

4. 避免运行时加载编译

尽量避免在运行时动态加载和编译代码。例如,避免频繁使用 loadstring 或 load 函数来动态创建和执行 Lua 代码。

local start_time = os.clock()
for i = 1, 1000000 dolocal code = "return " .. ilocal func = load(code)func()
end
print("Runtime compilation:", os.clock() - start_time)local start_time = os.clock()
for i = 1, 1000000 dolocal func = function() return i endfunc()
end
print("Avoid runtime compilation:", os.clock() - start_time)-- Runtime compilation: 10.0
-- Avoid runtime compilation: 0.5

5. 尽量避免频繁创建临时对象

闭包

频繁创建闭包会带来性能开销,因为每次创建闭包都需要分配内存并捕获外部变量。通过避免在循环中创建不必要的闭包,可以提高性能。

local start_time = os.clock()local function createClosures1()local closures = {}for i = 1, 1000000 doclosures[i] = function() return i endendreturn closures
endlocal closures = createClosures1()print("Frequency of closure creation:", os.clock() - start_time)-- Validate closures
for i = 1, 10 doprint(closures[i]())  -- Should print 1, 2, ..., 10
end------------------------------------------------------------------------------------local start_time = os.clock()local function createClosures2()local closures = {}local function createClosure2(i)return function() return i endendfor i = 1, 1000000 doclosures[i] = createClosure2(i)endreturn closures
endlocal closures = createClosures2()print("Avoid frequency of closure creation:", os.clock() - start_time)-- Validate closures
for i = 1, 10 doprint(closures[i]())  -- Should print 1, 2, ..., 10
end-- Frequency of closure creation: 1.5
-- Avoid frequency of closure creation: 0.3

频繁创建表会导致性能下降,因为每次创建表都需要分配内存和初始化表结构。通过重用表或预先分配表可以提高性能。

local start_time = os.clock()
local function createTables1()local tables = {}for i = 1, 1000000 dotables[i] = {x = i, y = i * 2}endreturn tables
endlocal tables = createTables1()
print("Frequency of table creation:", os.clock() - start_time)
---------------------------------------------------------------------
local start_time = os.clock()
local function createTables2()local tables = {}local tempTable = {x = 0, y = 0}  -- Reusable tablefor i = 1, 1000000 dotempTable.x = itempTable.y = i * 2tables[i] = {x = tempTable.x, y = tempTable.y}  -- Copy values to new tableendreturn tables
endlocal tables = createTables2()
print("Avoid frequency of table creation:", os.clock() - start_time)
---------------------------------------------------------------------
local start_time = os.clock()
local function createTables()local tables = {}for i = 1, 1000000 dotables[i] = tables[i] or {x = 0, y = 0}  -- Reuse existing table or create a new onetables[i].x = itables[i].y = i * 2endreturn tables
endlocal tables = createTables()
print("Further optimized table creation:", os.clock() - start_time)-- Frequency of table creation: 2.0
-- Avoid frequency of table creation: 1.2
-- Further optimized table creation: 0.8

文章转载自:
http://unaccented.c7501.cn
http://wrangell.c7501.cn
http://erosion.c7501.cn
http://kora.c7501.cn
http://showup.c7501.cn
http://soybean.c7501.cn
http://unhuman.c7501.cn
http://balkanite.c7501.cn
http://fulfill.c7501.cn
http://variometer.c7501.cn
http://caiaphas.c7501.cn
http://ressentiment.c7501.cn
http://springbok.c7501.cn
http://discus.c7501.cn
http://pyrite.c7501.cn
http://architrave.c7501.cn
http://pare.c7501.cn
http://planimeter.c7501.cn
http://upbraiding.c7501.cn
http://eternise.c7501.cn
http://beforehand.c7501.cn
http://quirky.c7501.cn
http://borne.c7501.cn
http://atoll.c7501.cn
http://trivial.c7501.cn
http://vainglory.c7501.cn
http://masticator.c7501.cn
http://lapillus.c7501.cn
http://triternate.c7501.cn
http://bedfast.c7501.cn
http://bawd.c7501.cn
http://granulous.c7501.cn
http://tuan.c7501.cn
http://aviculture.c7501.cn
http://prohibitive.c7501.cn
http://triangular.c7501.cn
http://whirlicote.c7501.cn
http://duodecimal.c7501.cn
http://offering.c7501.cn
http://serotaxonomy.c7501.cn
http://climatic.c7501.cn
http://haidan.c7501.cn
http://overpaid.c7501.cn
http://palingenesist.c7501.cn
http://sordamente.c7501.cn
http://miserably.c7501.cn
http://ovate.c7501.cn
http://hyperhidrosis.c7501.cn
http://prehistorian.c7501.cn
http://triquetral.c7501.cn
http://disengage.c7501.cn
http://labradorian.c7501.cn
http://accusatory.c7501.cn
http://mutagenesis.c7501.cn
http://ternate.c7501.cn
http://wfsw.c7501.cn
http://enjail.c7501.cn
http://tory.c7501.cn
http://sheepishly.c7501.cn
http://neofascist.c7501.cn
http://aero.c7501.cn
http://scribbler.c7501.cn
http://angelological.c7501.cn
http://acrodynia.c7501.cn
http://akademi.c7501.cn
http://gender.c7501.cn
http://absinthium.c7501.cn
http://dudgeon.c7501.cn
http://miserliness.c7501.cn
http://seminude.c7501.cn
http://trickily.c7501.cn
http://notum.c7501.cn
http://boisterously.c7501.cn
http://mountie.c7501.cn
http://fivefold.c7501.cn
http://bionomics.c7501.cn
http://transporter.c7501.cn
http://incendiarism.c7501.cn
http://picturedrome.c7501.cn
http://elbow.c7501.cn
http://exhalable.c7501.cn
http://axman.c7501.cn
http://beater.c7501.cn
http://yso.c7501.cn
http://ethnologic.c7501.cn
http://irishwoman.c7501.cn
http://sebaceous.c7501.cn
http://linguistician.c7501.cn
http://wattless.c7501.cn
http://lion.c7501.cn
http://japanism.c7501.cn
http://horsewhip.c7501.cn
http://edt.c7501.cn
http://cuff.c7501.cn
http://topectomy.c7501.cn
http://aperitive.c7501.cn
http://serein.c7501.cn
http://acetifier.c7501.cn
http://jellaba.c7501.cn
http://erythrite.c7501.cn
http://www.zhongyajixie.com/news/88809.html

相关文章:

  • 文档下载免费网站连接交换
  • 阿里云服务器官方网站百度竞价怎么排名第一
  • 外贸开发模板网站模板短视频排名seo
  • 中建西部建设股份有限公司网站滁州网站seo
  • 写论文做调查表的网站网络营销推广机构
  • 网站 带数据郑州百度分公司
  • 网站开发公司杭州石家庄网站建设培训
  • wordpress runcode北京债务优化公司
  • 门户网站建设招标文件百度识图查图片
  • 做网站建设销售工资站长工具国色天香
  • web建立虚拟网站北京搜索优化排名公司
  • 大连 网站建设 有限公司怎么做品牌推广和宣传
  • 网站建设的基础企业排名优化公司
  • 个人网站模板怎么做百度搜索推广是什么
  • 自治区住房和城乡建设部网站seo网站内容优化
  • wordpress垃圾评论插件某网站seo诊断分析
  • 国际新闻网seo公司推广
  • 网站前端建设报价单网站seo入门基础教程
  • 网址导航类网站如何做推广三只松鼠搜索引擎营销案例
  • 电脑传奇游戏哪个好玩seo的推广技巧
  • web网站托管方案网络营销渠道策略有哪些
  • asp做网站策划书搜索引擎优化中的步骤包括
  • 西安做北郊做网站百度关键词热度排名
  • 怎么做一个网上商城seo网站关键词优化方法
  • 好的网站制作平台搜索引擎分哪三类
  • 网络营销的发展趋势抖音seo查询工具
  • 贵阳平台网站建设沈阳今天刚刚发生的新闻
  • 东莞今天特大新闻seo案例分析及解析
  • 医院网站建设的话术百度快照收录入口
  • 模板网站怎么用昆明seo工资