LUA语言用不同方法实现格式化输出函数的效率对比

用不同的方法实现格式化输出函数的效率差距-LUA

最慢的方法-string.format

由于string.format函数需要将数据用tosring转为string类型之后再使用 . . 连接,因此速度会比 . . 更慢

local t_include = function(value, tab)				--通过判断table是否在scant中来判断该table是否已经输出过
	return tab[value]
end

local strfm = string.format               --常用string.format,将其赋值给一个局部变量防止每次运行程序都搜索一次

local retract                             --控制缩进
retract = function(plies)
	for i = 1,plies do
		io.write("\t")
	end
end


local printUntable                     --如果不是table则正常输出
printUntable = function(v ,  path , plies )
	if plies then
		retract(plies)
		print(string.format("[%s] = %s,\n" , tostring(path),tostring(v)))
	else
		if(type(v) == "string") then
			print(strfm("[%s] = \"%s\",\n",path,v))
		else
			print(strfm("[%s] = %s,\n",path,tostring(v)))
		end
	end
end
local printTable                          --打印table
printTable = function(scant , tab , plies , path)
	for i , v in pairs(tab) do
		if(type(v) == "table" ) then			--如果含有table
			if not t_include(tostring(v),scant) then		--判断是否以已经创存输出过
				scant[tostring(v)] = strfm("%s[%s]",path,tostring(v))
				retract(plies)
				print(strfm("[%s] = {\n" , tostring(i)))
				printTable(scant , v,plies+1,strfm("%s[%s]",path,tostring(v)))
				retract(plies)
				io.write("}," , "\n");
			else
				retract(plies)
				print(strfm("[%s] = %s,\n",tostring(i),scant[tostring(v)]))
			end
		else
		printUntable(v,i,plies)
		end
	end
end

prettystring = function(...)
	local arg = { ... }
	local path = "@\\"
	local scant = {}
	print("{\n");
	for i , v in pairs(arg) do
		if(type(v) == "table" ) then	--如果是table
			if not t_include(tostring(v),scant) then		--判断是否以已经创存输出过
				scant[tostring(v)] = strfm("%s[%s]",path,tostring(i))
				local plies = 1			--如果是table则创建一个scant来存储已有的table
				print(strfm("[%s] = {\n" , tostring(i)))
				printTable(scant,v,plies,path)			--打印table
				print("},\n");
			else
				print(strfm("[%s] = {%s},\n",tostring(i),scant[tostring(v)]))
			end
		else
			printUntable(v,i)
		end
	end
	io.write("}","\n");
end
return prettystring

. . 方法

速度偏慢与直接使用io.write输出差不多

local prettystring ,printTable ,printTable2,t_include

t_include = function(value, tab)				--通过判断table是否在scant中来判断该table是否已经输出过
for k,v in pairs(tab) do
  if v == value then
  return true;
  end
end
return false;
end

printTable = function(scant , tab , plies)
	for i , v in pairs(tab) do
		if(type(v) == "table" ) then			--如果含有table
			if not t_include(v,scant) then		--判断是否以已经创存输出过
				table.insert(scant,v)			--没有则加入scant
				for i = 1,plies do
					io.write("  ")
				end
				io.write("\[" .. tostring(i) .. "\] \= \{","\n");
				printTable(scant , v,plies+1)
				for i = 1,plies do
					io.write("  ")
				end
				io.write("\}," , "\n");
			end

		else
		for i = 1,plies do
			io.write("  ")
		end
		io.write("\[" .. tostring(i) .. "\] \= " .. tostring(v) .. "," , "\n")
		end
	end
end

prettystring = function(...)
	local arg = { ... }
	io.write("\{","\n");
	for i , v in ipairs(arg) do
		if(type(v) == "table" ) then	--如果是table
			local scant = {v}
			local plies = 1			--如果是table则创建一个scant来存储已有的table
			io.write("\[" .. tostring(i) .. "\] \= \{","\n");
			printTable(scant,v,plies)			--打印table
			io.write("\}," , "\n");
		else
			io.write(tostring(v) , "\n")  --如果不是table则转换成string输出
		end
	end
	io.write("\}","\n");
end
return prettystring

concat方法

该方法最快

local t_include = function(value, tab)				--通过判断table是否在scant中来判断该table是否已经输出过
	return tab[value]
end
local strfm = string.format
local TabInsert = table.insert
--[[
local TabInsert = function(self , value)
	io.write(value)
end
--]]
local Insert
Insert = function(pTab, key, value)
	TabInsert(pTab,"[")
	TabInsert(pTab,tostring(key))
	TabInsert(pTab,"] = ")
	if type(value) == "string" then
		TabInsert(pTab,"\"")
		TabInsert(pTab,value)
		TabInsert(pTab,"\"")
	else
		TabInsert(pTab,tostring(value))
	end
	TabInsert(pTab,",\n")
	return pTab
end

local InsertPath
InsertPath = function(pTab, key, value)
	TabInsert(pTab,"[")
	TabInsert(pTab,key)
	TabInsert(pTab,"] = ")
	TabInsert(pTab,"{")
	TabInsert(pTab,value)
	TabInsert(pTab,"}")
	TabInsert(pTab,",\n")
	return pTab
end



local retract                             --控制缩进
retract = function(pTab,plies)
	for i = 1,plies do
		TabInsert(pTab,"\t")
	end
	return pTab
end

local printTable                         --打印table
printTable = function(pTab , scannedTab , tab , plies , path)
	for i , v in pairs(tab) do
		if(type(v) == "table" ) then			--如果含有table
			if not t_include(tostring(v),scannedTab) then		--判断是否以已经创存输出过\
				pTab = retract(pTab , plies)
				TabInsert(pTab,"[")
				TabInsert(pTab,tostring(i))
				TabInsert(pTab,"] = ")
				TabInsert(pTab,"{\n")
				scannedTab[tostring(v)] = strfm("%s[%s]",path,tostring(i))
				pTab = printTable(pTab,scannedTab , v,plies+1,strfm("%s[%s]",path,tostring(v)))
				pTab = retract(pTab , plies)
				TabInsert(pTab,"},\n" );
			else
				pTab = retract(pTab , plies)
				pTab = InsertPath(pTab , tostring(i),scannedTab[tostring(v)])
			end
		else
		pTab = retract(pTab , plies)
		pTab = Insert(pTab , i,v)
		end
	end
	return pTab
end

prettystring = function(...)
	local arg = { ... } or {}
	local path = "@\\"
	local scannedTab = {}
	local pTab = {}
	TabInsert(pTab,"{\n");
	for i = 1 , select('#', ... ) do
		if(type(arg[i]) == "table" ) then
			if not t_include(tostring(v),scannedTab) then
				TabInsert(pTab,"[")
				TabInsert(pTab,tostring(i))
				TabInsert(pTab,"] = ")
				TabInsert(pTab,"{\n")
				scannedTab[tostring(v)] = strfm("%s[%s]",path,tostring(i))
				local plies = 1													--如果是table则创建一个scant来存储已有的table
				pTab = printTable(pTab,scannedTab,arg[i],plies,strfm("%s[%s]",path,tostring(i)))			            --打印table
				TabInsert(pTab,"},\n")
			else
				pTab = InsertPath(pTab, i, scannedTab[tostring(arg[i])])
			end
		else
			pTab = Insert(pTab, i, arg[i])
		end
	end
	TabInsert(pTab,"}\n");
	io.write(table.concat(pTab))
end

return prettystring

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值