Skip to content

Instantly share code, notes, and snippets.

@Jerakin
Created August 10, 2020 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jerakin/5764997c9de6936c7e4a5e6db721b511 to your computer and use it in GitHub Desktop.
Save Jerakin/5764997c9de6936c7e4a5e6db721b511 to your computer and use it in GitHub Desktop.
Quick profiling
local md5 = require "main.md5"
local by_index = {}
local by_id = {}
local function time_it(fnc)
local before = os.clock()
fnc()
print((os.clock() - before)*1000)
end
local function find_by_id(find_this)
return by_id[find_this]
end
local function find_by_index(find_this)
local id
for i=1, #by_index do
id = by_index[i]
if find_this == id then
return true
end
end
return false
end
local function loop_all_index()
local id
local _trash = ""
for i=1, #by_index do
id = by_index[i]
_trash = "well" .. "hi" .. "there"
end
return true
end
local function loop_all_id()
local _trash = ""
for id, _ in pairs(by_id) do
_trash = "well" .. "hi" .. "there"
end
return true
end
function init(self)
local before = os.clock()
local total_elements = 1000000
local m
local id
for i=1, total_elements do
m = md5.new()
m:update(tostring(i))
id = md5.tohex(m:finish())
by_id[id] = true
by_index[#by_index + 1] = id
end
print("Setup", (os.clock() - before)*1000)
local first = by_index[1]
local mid = by_index[total_elements/2]
local last = by_index[total_elements]
print("find_by_id: first")
time_it(function() find_by_id(first) end)
print("find_by_id: mid")
time_it(function() find_by_id(mid) end)
print("find_by_id: last")
time_it(function() find_by_id(last) end)
print("find_by_index: first")
time_it(function() find_by_index(first) end)
print("find_by_index: mid")
time_it(function() find_by_index(mid) end)
print("find_by_index: last")
time_it(function() find_by_index(last) end)
print("loop: loop_all_id")
time_it(loop_all_id)
print("loop: loop_all_index")
time_it(loop_all_index)
end
@Jerakin
Copy link
Author

Jerakin commented Aug 11, 2020

Did some more tests, had to keep it at 1m to even get some numbers on it. Anything lower and most stuff wouldn't show any thing. So first observation: The table need to have a lot of information in it to actually matter much.

Removing with an index (Pokemon number 123121 or something) was a lot faster in an indexed list but not by much at all. When removing by ID the hash table was a lot quicker, practically instant (showed 0 on the test) while it took a lot of time for the indexed table (24-35). Later test have also shown that that looping over an indexed table can also be slow.

I think we might be optimizing when there is no need either. But for now I will change it to use the "hashed" list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment