diff options
| author | william <[email protected]> | 2014-02-02 17:59:48 -0800 |
|---|---|---|
| committer | william <[email protected]> | 2014-02-02 17:59:48 -0800 |
| commit | ff5055150c526141ce670715ba13e41dd0bf3f16 (patch) | |
| tree | 2eaa0867e9e09746384ad913cc7b8e54885d7b8a | |
| parent | a605dc78b1e634d76ce5a3ae087676862c9576ea (diff) | |
use system monotonic time instead of clock()
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | bench.c | 28 |
2 files changed, 33 insertions, 2 deletions
@@ -34,8 +34,13 @@ else SOFLAGS = -shared endif +ifeq ($(shell -uname -s), Linux) +LIBS = -lrt +endif + + bench.so: bench.c - $(CC) -o $@ $< $(CPPFLAGS) -DLUA_COMPAT_ALL $(CFLAGS) -Wno-unused-function $(SOFLAGS) + $(CC) -o $@ $< $(CPPFLAGS) -DLUA_COMPAT_ALL $(CFLAGS) -Wno-unused-function $(SOFLAGS) $(LIBS) bench-wheel8.so: CPPFLAGS+=-DWHEEL_BIT=3 -DWHEEL_NUM=$(WHEEL_NUM) @@ -5,6 +5,10 @@ #include <unistd.h> #include <dlfcn.h> +#if __APPLE__ +#include <mach/mach_time.h> +#endif + #include <lua.h> #include <lualib.h> #include <lauxlib.h> @@ -24,11 +28,29 @@ struct bench { struct timeout *timeout; struct benchops ops; timeout_t curtime; + +#if __APPLE__ + mach_timebase_info_data_t timebase; +#endif }; /* struct bench */ static int bench_clock(lua_State *L) { - lua_pushnumber(L, (double)clock() / CLOCKS_PER_SEC); +#if __APPLE__ + struct bench *B = lua_touserdata(L, 1); + unsigned long long abt; + + abt = mach_absolute_time(); + abt = abt * B->timebase.numer / B->timebase.denom; + + lua_pushnumber(L, (double)abt / 1000000000L); +#else + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + + lua_pushnumber(L, (double)ts.tv_sec + ((double)ts.tv_nsec / 1000000000L)); +#endif return 1; } /* bench_clock() */ @@ -45,6 +67,10 @@ static int bench_new(lua_State *L) { B = lua_newuserdata(L, sizeof *B); memset(B, 0, sizeof *B); +#if __APPLE__ + mach_timebase_info(&B->timebase); +#endif + luaL_getmetatable(L, "BENCH*"); lua_setmetatable(L, -2); |
