Last active
February 16, 2024 09:46
-
-
Save thelindat/939fb0aef8b80a077f76f1a850b2a53d to your computer and use it in GitHub Desktop.
Basic benchmarking for LuaGLM 5.4 (FiveM).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
db
hash
iterators