Skip to content

Instantly share code, notes, and snippets.

@thelindat
Last active February 16, 2024 09:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thelindat/939fb0aef8b80a077f76f1a850b2a53d to your computer and use it in GitHub Desktop.
Save thelindat/939fb0aef8b80a077f76f1a850b2a53d to your computer and use it in GitHub Desktop.
Basic benchmarking for LuaGLM 5.4 (FiveM).
local curtime = os.nanotime
local results = {}
---@param iterations number
---@param title string | number
---@param func function
local function benchmark(iterations, title, func)
local start = curtime()
for i = 1, iterations do
func()
end
results[#results + 1] = { (curtime() - start) / iterations, title }
end
---@param iterations number
local function showResults(iterations)
print(('Average results from %d iterations (ms)'):format(iterations))
table.sort(results, function(a, b) return a[1] < b[1] end)
for i = 1, #results do
local result = results[i]
print(('#%d - %.4f\t(%s)'):format(i, result[1] / 1e6, result[2]))
end
table.wipe(results)
end
---@param n number
---@param tbl table<string, function>
return function(n, tbl)
for k, v in pairs(tbl) do
benchmark(n, k, v)
end
showResults(n)
end
local benchmark = require 'benchmark'
for i = 1, 10000 do
local key = ('testkey:%d'):format(i)
local value = ('bob:%d'):format(i)
SetResourceKvp(key, value)
end
benchmark(100, {
kvp = function()
local kvpHandle = StartFindKvp('testkey:')
if kvpHandle ~= -1 then
local keys = {}
repeat
local key = FindKvp(kvpHandle)
if key then
keys[#keys + 1] = key
end
until not key
EndFindKvp(kvpHandle)
end
end,
mysql = function()
MySQL.query.await('SELECT username FROM test_table LIMIT 10000')
end
})
local benchmark = require 'server.benchmark'
local function hashstring(str)
local hash = 0
for i = 1, #str do
hash = hash + str:sub(i, i):lower():byte() & 0xFFFFFFFF
hash = hash + (hash << 10) & 0xFFFFFFFF
hash = hash ~ (hash >> 6) & 0xFFFFFFFF
end
hash = hash + (hash << 3) & 0xFFFFFFFF
hash = hash ~ (hash >> 11) & 0xFFFFFFFF
hash = hash + (hash << 15) & 0xFFFFFFFF
if hash > 2147483647 then
return hash - 4294967295
end
return hash
end
local joaat = joaat
local GetHashKey = GetHashKey
benchmark(100, {
hashstring = function()
hashstring('Hello, World!')
end,
joaat = function()
joaat('Hello, World!')
end,
GetHashKey = function()
GetHashKey('Hello, World!')
end
})
local benchmark = require 'benchmark'
local data = {}
for i = 1, 10000 do
data[i] = 1
end
local x
benchmark(100, {
numeric = function()
for i = 1, #data do
x = data[i]
end
end,
next = function()
for i, v in next, data do
x = v
end
end,
pairs = function()
for i, v in pairs(data) do
x = v
end
end,
ipairs = function()
for i, v in ipairs(data) do
x = v
end
end,
each = function()
for i, v in each(data) do
x = v
end
end,
})
@thelindat
Copy link
Author

thelindat commented Aug 6, 2022

db

Average results from 100 iterations (ms)
#1 - 5.0075      (kvp)
#2 - 13.1271     (mysql)

hash

Average results from 100 iterations (ms)
#1 - 0.0001      (GetHashKey)
#2 - 0.0004      (joaat)
#3 - 0.0025      (hashstring)

iterators

Average results from 100 iterations (ms)
#1 - 0.0919      (numeric)
#2 - 0.2324      (ipairs)
#3 - 0.2542      (pairs)
#4 - 0.2597      (next)
#5 - 0.2671      (each)

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