lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Alex Bilyk wrote:
>>>
> I have been doing some perfomance tests for LUA-based code and fould that
standard LUA *clock()* function yields just about exactly half the time it is
supposed to. I have AMD 1.3G system running Win2K. Granted, I measured it
against the standard Win2K clock application (it would count one day for two,
and it does not, if it were at fault). Has anyubody experienced this problem?
<<<

No. I am not sure to understand the way you are testing it, but I tried it
against my own functions using the Pentium performance counter and I get
similar results.

FYI, I give my routines here, hoping they can be useful.
I am not sure if I really need two functions, it seemed practical at the
time...

/* Performance Counter (fine time) */
/* It is based on Windows' performance counter, which is available
 * on all versions of Win32, but only on Pentium class processors.
 * To handle all cases, we can fall back on the less precise but more
 * classical clock() call, or force the use of this function.
 */
static BOOL gs_bUsePerformanceCounter;
static INT64_t gs_performanceCounterBeginTime;
static INT64_t gs_performanceCounterFrequency;

/**
 * Initialization of global variables.
 */
void lwGlobalInitialization()
{
	g_appName = "LuaWin32";

	gs_bUsePerformanceCounter = TRUE;
	gs_performanceCounterBeginTime = 0;
	gs_performanceCounterFrequency = 0;
}

/**
 * Start to count time using, if requested, the Pentium performance counter.
 * The optional argument, defaulting to TRUE, can be used (if set to FALSE)
 * to force the use of the clock() function instead.
 * @return the current value of the counter and the frequency.
 */
int lwStartFineClock(lua_State *L)
{
	if (lua_gettop(L) == 0)	/* No argument */
	{
		gs_bUsePerformanceCounter = TRUE;	/* Try to use performance counter by
default */
	}
	else
	{
		gs_bUsePerformanceCounter = lua_toboolean(L, 1);
	}
	/* If first call */
	if (gs_performanceCounterFrequency == 0)
	{
		/* If used, get the frequency of the counter */
		if (gs_bUsePerformanceCounter)
		{
			BOOL result;
			result = QueryPerformanceFrequency((LARGE_INTEGER
*)&gs_performanceCounterFrequency);
			if (!result)
			{
				gs_bUsePerformanceCounter = FALSE;
			}
		}
		if (!gs_bUsePerformanceCounter)
		{
			gs_performanceCounterFrequency = CLOCKS_PER_SEC;
		}
	}
	/* Get the starting counter value */
	if (gs_bUsePerformanceCounter)
	{
		QueryPerformanceCounter((LARGE_INTEGER *)&gs_performanceCounterBeginTime);
	}
	else
	{
		gs_performanceCounterBeginTime = clock();
	}

	lua_pushnumber(L, (double)gs_performanceCounterBeginTime);
	lua_pushnumber(L, (double)gs_performanceCounterFrequency);
	return 2;
}

/**
 * Get the elapsed time since the last call.
 * The optional argument, defaulting to TRUE, can be used
 * to reset the counter, ie. this call starts the counter again.
 * Otherwise (if set to FALSE), the counter continues from the previous
 * reseting call.
 * @return the elapsed time in seconds, in microseconds, and the current
 * value of the counter.
 */
int lwGetFineClockTime(lua_State *L)
{
	BOOL bResetTimer;
	INT64_t endTime;
	INT64_t elapsed;

	if (lua_gettop(L) == 0)	/* No argument */
	{
		bResetTimer = TRUE;
	}
	else
	{
		bResetTimer = lua_toboolean(L, 1);
	}

	/* Get the ending counter value */
	if (gs_bUsePerformanceCounter)
	{
		QueryPerformanceCounter((LARGE_INTEGER *)&endTime);
	}
	else
	{
		endTime = clock();
	}

	/* Determine the elapsed counts */
	elapsed = endTime - gs_performanceCounterBeginTime;

	/* Reset counter if requested */
	if (bResetTimer)
	{
		gs_performanceCounterBeginTime = endTime;
	}

	/* Convert counts to time in seconds and return it */
	lua_pushnumber(L, (double)elapsed /
(double)gs_performanceCounterFrequency);
	/* Convert counts to time in microseconds and return it */
	lua_pushnumber(L, (1000000.0 * (double)elapsed) /
(double)gs_performanceCounterFrequency);
	/* Return current count */
	lua_pushnumber(L, (double)endTime);

	return 3;
}

I first wrote these functions to have finer time measuring of Lua
performances, then I went on and started to add other Win32 functions...
When all this will mature, I will release it as open source (zlib/png
licence).

Regards.

-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--

GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net