Skip to content

Instantly share code, notes, and snippets.

@CapsAdmin
Last active February 27, 2022 20:48
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 CapsAdmin/a66e08ad9cd2bc69f9d225242095c405 to your computer and use it in GitHub Desktop.
Save CapsAdmin/a66e08ad9cd2bc69f9d225242095c405 to your computer and use it in GitHub Desktop.
git clone git@github.com:LuaJIT/LuaJIT.git
cd LuaJIT
make
cd ..
gcc -O2 main.c -I LuaJIT/src/ -L LuaJIT/src/ -lluajit
./a.out main.lua
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <luajit.h>
static int isstring(lua_State *L) {
lua_pushboolean(L, lua_isstring(L, 1));
return 1;
}
int main(int argc, char *argv[])
{
int status;
lua_State *L;
L = luaL_newstate(); // open Lua
if (!L) {
return -1; // Checks that Lua started up
}
luaL_openlibs(L); // load Lua libraries
lua_register(L, "isstring", isstring);
if (argc > 1) {
status = luaL_loadfile(L, argv[1]); // load Lua script
int ret = lua_pcall(L, 0, 0, 0); // tell Lua to run the script
if (ret != 0) {
fprintf(stderr, "%s\n", lua_tostring(L, -1)); // tell us what mistake we made
return 1;
}
}
lua_close(L); // Close Lua
return 0;
}
local SysTime = _G.os.clock
local type = _G.type
local isstring = _G.isstring
local function benchmark(func)
local count = 500000000 -- increase the iterations
local StartTime = SysTime()
for i = 1, count do
func() -- don't bother passing arguments, it adds unesseceary time to the profiling
end
local EndTime = SysTime()
return EndTime - StartTime .. " secs."
end
local testString = "jadevx4bo7nb7mof58lirs2im9fufbz5"
local function bench0()
return type(testString) == "string"
end
local function bench1()
return isstring(testString)
end
jit.on()
print("Type (JIT On): " .. benchmark(bench0))
print("isstring (JIT On): " .. benchmark(bench1))
jit.off()
print("Type (JIT Off): " .. benchmark(bench0))
print("isstring (JIT Off): " .. benchmark(bench0))
jit.on() -- Turn jit it back on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment