Skip to content

Instantly share code, notes, and snippets.

View DarkWiiPlayer's full-sized avatar
💭
OwO

Wii DarkWiiPlayer

💭
OwO
View GitHub Profile
@DarkWiiPlayer
DarkWiiPlayer / bench.lua
Last active April 30, 2022 13:54
Lua benchmark: closures vs. coroutines
local times = 100000
local start = os.clock()
for i=1,times do
local closure = function()
print("test")
end
end
print(os.clock() - start)
local function locals(n)
n = n + 1
local result = {}
for i=1,math.huge do
name, value = debug.getlocal(n, i)
if not name then break end
result[name] = {value}
end
return result
end
#!/usr/bin/env luajit
local c = require 'ansicolors'
local pegasus = require 'pegasus'
local http = require 'socket.http'
local ip = http.request('http://darkwiiplayer.com/api/ip')
math.randomseed(os.time())
@DarkWiiPlayer
DarkWiiPlayer / bench.lua
Last active May 16, 2019 12:16
Benchmark for memoization of a simple index translation function
local ffi = require 'ffi'
local intidx_fnc do
if ffi.abi('le') then
function intidx_fnc(n)
if n<1 or n>4 then
error("wrong index", 2)
end
return n-1
end
@DarkWiiPlayer
DarkWiiPlayer / greeter.terra
Created July 17, 2019 11:44
A simple Lua "C" extension that's actually written in terra.
local stdio = terralib.includec 'stdio.h'
local lua = terralib.includec 'lua.h'
-- lua_tolstring expects a size_t, which terra doesn't know.
local lua_length = lua.lua_tolstring:gettype().parameters[3].type
-- Fixed by introspecting the function and just reading the type
local terra hello(L : &lua.lua_State)
var len : lua_length;
var string : rawstring;
@DarkWiiPlayer
DarkWiiPlayer / coro.lua
Last active August 1, 2019 15:44
Lua Coroutines vs. Ruby Fibers benchmark
local num = 2e6 -- 2e4
print("Running with "..math.floor(tostring(num)).." Coroutines\n")
local threads = {}
local t1 = os.clock()
for i=1,num do
-- Creates a closure for every coroutine
-- The optimization is trivial, but was left out to be fair, as ruby deals poorly with lambdas
local function element(name)
return function(tab)
return function()
print('<'..name..'>')
if type(tab) == 'table' then
for key, value in ipairs(tab) do
value()
end
elseif type(tab) == 'string' then
print(tab)
local function tmap(tab, func)
local new = {}
for key, value in pairs(tab) do
new[key] = func(value)
end
return new
end
local terra add(a : int, b : int)
return a + b
local doubles do
local function iter(tab, current)
local index = (current+1)*2
local first, second = tab[index-1], tab[index]
if first and second then
return current+1, first, second
else
return
end
end
local lfs = require 'lfs'
local commands = {}
setmetatable(commands, {__call = function(self, name, func, description)
if func then
self[name] = func
table.insert(self, {name=name, description=description})
else
for index, command in ipairs(self) do
print(command.name .. ': ' .. command.description)