Skip to content

Instantly share code, notes, and snippets.

@ArmoredPony
Last active September 27, 2023 00:30
Show Gist options
  • Save ArmoredPony/107876f6a3fb775e3e9e738ecc64031f to your computer and use it in GitHub Desktop.
Save ArmoredPony/107876f6a3fb775e3e9e738ecc64031f to your computer and use it in GitHub Desktop.
Simple Lua binding for QueryPerformanceCounter function from WinAPI. Returns a function, not a table.
#include "lua.h"
#include <windows.h>
static LARGE_INTEGER pc, pf;
static int queryperformancecounter(lua_State* L) {
QueryPerformanceCounter(&pc);
lua_pushnumber(L, (double)pc.QuadPart / pf.QuadPart);
return 1;
}
int luaopen_queryperformancecounter(lua_State* L) {
QueryPerformanceFrequency(&pf);
lua_pushcfunction(L, queryperformancecounter);
return 1;
}
// Compile with:
// gcc -O2 -shared -fPIC -o queryperformancecounter.dll lqueryperformancecounter.c <path/to/lua>/bin/lua54.dll -I<path/to/lua>/include
// Usage:
// local qpc = require 'queryperformancecounter'
// local t = qpc()
// print(qpc() - t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment