Skip to content

Commit

Permalink
Perf: Add ad-hoc benchmarking function wrapper.
Browse files Browse the repository at this point in the history
Expose native timer functions to Lua.
  • Loading branch information
deplinenoise committed Sep 1, 2013
1 parent 6fb929f commit 7cbc64a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions scripts/tundra/boot.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module(..., package.seeall)

-- Use "strict" when developing to flag accesses to nil global variables
-- This has very low perf impact (<0.1%), so always leave it on.
require "strict"

local os = require "os"
Expand All @@ -9,6 +10,7 @@ local util = require "tundra.util"
local depgraph = require "tundra.depgraph"
local unitgen = require "tundra.unitgen"
local buildfile = require "tundra.buildfile"
local native = require "tundra.native"

-- This trio is so useful we want them everywhere without imports.
function _G.printf(msg, ...)
Expand All @@ -27,6 +29,22 @@ function _G.croak(msg, ...)
os.exit(1)
end

-- Expose benchmarking function for those times everything sucks
--
-- Wrap a function so that it prints execution times.
--
-- Usage:
-- foo = bench("foo", foo) -- benchmark function foo
function _G.bench(name, fn)
return function (...)
local t1 = native.get_timer()
local result = { fn(...) }
local t2 = native.get_timer()
printf("%s: %ss", name, native.timerdiff(t1, t2))
return unpack(result)
end
end

local environment = require "tundra.environment"
local nodegen = require "tundra.nodegen"
local decl = require "tundra.decl"
Expand Down
19 changes: 19 additions & 0 deletions src/LuaInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,23 @@ static int LuaWin32RegisterQuery(lua_State *L)
}
#endif

static int LuaTime(lua_State* L)
{
lua_pushinteger(L, TimerGet());
return 1;
}

static int LuaTimeDiff(lua_State* L)
{
lua_Integer a = luaL_checkinteger(L, 1);
lua_Integer b = luaL_checkinteger(L, 2);
double d = t2::TimerDiffSeconds(a, b);
char buffer[64];
snprintf(buffer, sizeof buffer, "%.5f", d);
lua_pushstring(L, buffer);
return 1;
}

static const luaL_Reg s_LuaFunctions[] = {
{ "exit", LuaTundraExit },
{ "list_directory", LuaListDirectory },
Expand All @@ -482,6 +499,8 @@ static const luaL_Reg s_LuaFunctions[] = {
{ "djb2_hash_path", LuaDjb2HashPath },
{ "getcwd", LuaGetCwd },
{ "mkdir", LuaMkdir },
{ "get_timer", LuaTime },
{ "timerdiff", LuaTimeDiff },
#ifdef _WIN32
{ "reg_query", LuaWin32RegisterQuery },
#endif
Expand Down

0 comments on commit 7cbc64a

Please sign in to comment.