Я люблю Lua. I love Lua.

Скорость загрузки скриптов Lua

Posted in Uncategorized by ilovelua on 10 апреля, 2013

Тут на работе возник вопрос: насколько обильные комментарии замедляют скорость загрузки скриптов? Ответ: весьма незначительно. Ниже тест:

local testFolder = ...

require('lfs')

local folderCommented = testFolder .. 'commented/'

lfs.mkdir(folderCommented)

local folderUncommented = testFolder .. 'uncommented/'

lfs.mkdir(folderUncommented)

local commentedText = [[
-------------------------------------------------------------------------------
-- Удаляет подпись к заданной точке маршрута с карты.
function remove_waypoint_text%d(wpt)
  set_mapObjects(wpt.boss.mapObjects.route.numbers[wpt.index].id, nil)
  return table.remove(wpt.boss.mapObjects.route.numbers, wpt.index)
end

-------------------------------------------------------------------------------
--
function set_mapObjects%d(a_key, a_value)
	mapObjects[a_key] = a_value
end
]]

local fileCount = 100
local textCount = 100

for i = 1, fileCount do
	local filename = string.format('%s%03d.lua', folderCommented, i)
	local file = io.open(filename, 'w')
	for j = 1, textCount do
		-- зададим жару Lua - пусть все функции будут разными
		file:write(string.format(commentedText, j, j))
	end	
	file:close()
end

local uncommentedText = [[
function remove_waypoint_text%d(wpt)
  set_mapObjects(wpt.boss.mapObjects.route.numbers[wpt.index].id, nil)
  return table.remove(wpt.boss.mapObjects.route.numbers, wpt.index)
end

function set_mapObjects%d(a_key, a_value)
	mapObjects[a_key] = a_value
end
]]

for i = 1, fileCount do
	local filename = string.format('%s%03d.lua', folderUncommented, i)
	local file = io.open(filename, 'w')
	for j = 1, textCount do
		-- зададим жару Lua - пусть все функции будут разными
		file:write(string.format(uncommentedText, j, j))
	end	
	file:close()
end

-- загружаем файлы с комментариями
local startTime = os.clock()

for i = 1, fileCount do
	local filename = string.format('%s%03d.lua', folderCommented, i)
	dofile(filename)
end

local commentedTime = os.clock() - startTime
print('commented files loading time:', commentedTime)

-- загружаем файлы без комментариев
local startTime = os.clock()

for i = 1, fileCount do
	local filename = string.format('%s%03d.lua', folderUncommented, i)
	dofile(filename)
end

local uncommentedTime = os.clock() - startTime

print('uncommented files loading time:', uncommentedTime)
print(string.format('loading speed difference is: %0.2f%%', 100 - (uncommentedTime / commentedTime) * 100))

local commentedFilesSize = 4728400

print('total commented files size is %d bytes:', commentedFilesSize)

local uncommentedFilesSize =  2558400

print('total uncommented files size is %d bytes:', uncommentedFilesSize)

print(string.format('files size difference is: %0.2f%%', 100 - (uncommentedFilesSize / commentedFilesSize) * 100))

Запускаем:

commented files loading time: 0.382
uncommented files loading time: 0.363
loading speed difference is: 4.97%
total commented files size is %d bytes: 4728400
total uncommented files size is %d bytes: 2558400
files size difference is: 45.89%

Итого: комментированный фалов почти 4 с половиной мегабайта, некомментированных почти в 2 раза меньше. Разница в скорости загрузки ~5 процентов.

Резюме: о комментариях в коде можно не волноваться.