Hi folks,
I was wondering how one could make some accurate time measurements ?
My question Lua-related, not Löve related, or i'll just use love.timer.
Say, for instance, that I'll need to benchmark the execution time of function.
Problem is, Lua's operating system facilities offers me os.clock, os.difftime, os.time, but they all works with seconds.
What if i need milliseconds, or less ?
Is there some hacky stuff, or should I rely on external libs ?
Thanks in advance.
How would you make accurate time measurements using Lua ?
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: How would you make accurate time measurements using Lua
os.clock is in seconds, but usually has millisecond precision. The C standard says it should be the best CPU time approximation and Linux-based stuff (and OS X, I think) does this just fine. Windows returns realtime instead. It can be used to do benchmarking, but you should not compare the results across platforms.
If you need microsecond precision, you have to reach out to the OS API. I made myself a little module for this. Should work on Windows and Linux (link against librt if monotonic timers are available). I don't have OS X so I didn't implement its monotonic timer, but the gettimeofday function is available there.
This is just for getting the value of a timer with an undefined starting time. There may be more advanced timer Lua modules out there. LuaSocket also has a realtime timer with millisecond precision if that's already part of your script.
If you need microsecond precision, you have to reach out to the OS API. I made myself a little module for this. Should work on Windows and Linux (link against librt if monotonic timers are available). I don't have OS X so I didn't implement its monotonic timer, but the gettimeofday function is available there.
Code: Select all
#ifdef _WIN32
#include <Windows.h>
#define EXPORT_DECL __declspec(dllexport)
#else
#include <time.h>
#include <sys/time.h>
#define EXPORT_DECL
#endif
#include <lua.h>
#ifdef _WIN32
static double timer_resolution = 0.0;
#endif
static int microtime_query(lua_State * L)
{
lua_Number time = 0;
#ifdef _WIN32
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
time = (lua_Number)(counter.QuadPart * timer_resolution);
#elif _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC_RAW) || defined(CLOCK_MONOTONIC)
struct timespec t;
#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &t);
#else
clock_gettime(CLOCK_MONOTONIC, &t);
#endif
time = (lua_Number)((double)t.tv_sec + (double)t.tv_nsec / 1000000000.0);
#else
struct timeval t;
gettimeofday(&t, NULL);
time = (lua_Number)((double)t.tv_sec + (double)t.tv_usec / 1000000.0);
#endif
lua_pushnumber(L, time);
return 1;
}
EXPORT_DECL int luaopen_microtime(lua_State * L)
{
#ifdef _WIN32
LARGE_INTEGER frequency;
int support = QueryPerformanceFrequency(&frequency);
if (!support)
{
// No high-resolution counter found.
lua_pushnil(L);
return 1;
}
timer_resolution = 1.0 / frequency.QuadPart;
#endif
lua_pushcfunction(L, µtime_query);
return 1;
}
Shallow indentations.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: How would you make accurate time measurements using Lua
Wow.
Gratitutes, Boolean dude.
Yes, I've been digging in the mid-time. I was more looking for some tool in plain Lua, but it seems there isn't.
Thanks for the C-snippet, and for the LuaSocket tip.
Gratitutes, Boolean dude.
Yes, I've been digging in the mid-time. I was more looking for some tool in plain Lua, but it seems there isn't.
Thanks for the C-snippet, and for the LuaSocket tip.
Re: How would you make accurate time measurements using Lua
This doesn't work for you?
https://love2d.org/wiki/love.timer.getMicroTime
Edit: Sorry, I didn't realize that this was about Lua and not Love.
https://love2d.org/wiki/love.timer.getMicroTime
Edit: Sorry, I didn't realize that this was about Lua and not Love.
Who is online
Users browsing this forum: No registered users and 2 guests