With no overhead from Minetest
I ran hudkit.exists and hudkit.change 1,000,000,000 times -
a billion times - in luajit, and it took just 0.39 seconds. This was with Luajit on the command line and with hud_change calls removed. (so we're only testing the things around it).
This gives each exists and change pair of calls a time of 0.00000000039 seconds each, on average. (3.9*10^-10 s)
With 10,000,000 calls, for comparison with below, it took 0.01 seconds. (the finest os.clock can measure is 0.01, so this could be anytime above 5 millisecs, I guess.)
With overhead from Minetest
I ran hudkit.exists and hudkit.change 10,000,000 times -
ten million times - in Minetest's lua environment, and it took 4.72 seconds. This was with hud_change calls removed. (so we're only testing the things around it).
This gives each exists and change pair of calls a time of 0.000000472 seconds. (4.72*10^-7 s)
10,000,000: 4.72s
01,000,000: 0.39s
00,100,000: 0.01s (the finest os.clock can measure is 0.01)
Summary
I very much doubt that you'll have more than 1000 checks of these per second, and that would be a fraction of a millisecond.
Code: Select all
-- HudKit, by rubenwardy
-- License: Either WTFPL or CC0, you can choose.
local function hudkit()
return {
players = {},
add = function(self, player, id, def)
local name = player:get_player_name()
local elements = self.players[name]
if not elements then
self.players[name] = {}
elements = self.players[name]
end
elements[id] = player:hud_add(def)
end,
exists = function(self, player, id)
local elements = self.players[player:get_player_name()]
return elements and elements[id]
end,
change = function(self, player, id, stat, value)
local elements = self.players[player:get_player_name()]
if not elements or not elements[id] then
return false
end
return true
end,
remove = function(self, player, id)
local elements = self.players[player:get_player_name()]
if not elements or not elements[id] then
return false
end
elements[id] = nil
return true
end
}
end
local myhudkit = hudkit()
player = {
get_player_name = function(self)
return "player1"
end,
hud_add = function(self, def)
return 1
end,
hud_change = function(self, one, two, three)
end,
hud_remove = function(self, id)
end
}
-- Please note, passing a nil player value will cause HudKit
-- to crash (it does not check for that).
local x = os.clock()
myhudkit:add(player, "modname:hud_1", {
hud_elem_type = "text",
position = {x = 1, y = 0},
scale = {x = 100, y = 100},
text = some_data,
offset = {x=-100, y = 20}
})
local some_data = "abcd"
for i=1, 10000000 do
if myhudkit:exists(player, "modname:hud_1") then
myhudkit:change(player, "modname:hud_1", "text", some_data)
else
print("error")
end
end
print(string.format("elapsed time: %.2f\n", os.clock() - x))
4aiman wrote:Bear in mind that it's not the case where we can simply use a for clause.
Statistics should be true to life with all those other mods enabled -- It may well be that this code will affect some other mod which won't be able to do its stuff in time.
That's bollocks. Sorry, but it is.