lua-users home
lua-l archive

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


Friday, March 3, 2006, 3:43:05 PM, Matt Campbell wrote:

> I noticed that LuaProfiler uses the clock() function for timing by
> default.  On Windows (at least with Microsoft Visual C++), a clock is
> one millisecond -- much less than on Linux and other Unix systems.  Does
> this mean that function timing is significantly more distorted on 
> Windows than on Linux, particularly for very fast functions that are
> called many times?  If so, what can be done about this?  Thanks.

Fun with lua_tcc...

timer = tcc.compile ([[
  #include "lua.h"
  #include "winapi/windows.h"
  int getraw (lua_State *L) {
    LARGE_INTEGER t;
    QueryPerformanceCounter(&t);
    //lua_pushnumber (L, (double )t.QuadPart);
    lua_pushnumber (L, (double)t.u.LowPart + ((double )t.u.HighPart * (double )4294967296.0));
    return 1;
  }
  int getfreq (lua_State *L) {
    LARGE_INTEGER t;
    QueryPerformanceFrequency(&t);
    //lua_pushnumber (L, (double )t.QuadPart);
    lua_pushnumber (L, (double)t.u.LowPart + ((double )t.u.HighPart * (double )4294967296.0));
    return 1;
  }
   int getsec (lua_State *L) {
    static int getsec_init = 0;
    static double getsec_freq;
    LARGE_INTEGER t;
    if (!getsec_init) {
        QueryPerformanceFrequency(&t);
        getsec_freq = (double)t.u.LowPart + ((double )t.u.HighPart * (double )4294967296.0);
        if (getsec_freq == 0) return 0;
        getsec_init = 1;
    }
    QueryPerformanceCounter(&t);
    //lua_pushnumber (L, (double )t.QuadPart);
    lua_pushnumber (L, (double)(t.u.LowPart + ((double )t.u.HighPart * (double )4294967296.0)) / getsec_freq);
    return 1;
  }
]], {"getraw", "getfreq", "getsec"}, {"lua51", "kernel32"})

function SmallestDiff (n,m)
    local t0 = timer.getsec()
    local t1
    local mint = 100000
    for i = 1 , n do
        -- t1 = timer.getsec()
        for j = 1, m do t1 = timer.getsec() end
        if (t1 - t0) < mint then mint = t1 - t0 end
        t0 = t1
    end
    return mint
end

> =SmallestDiff(10,1)
2.7936475817114e-006
> =SmallestDiff(10,10)
2.3466651327908e-005
> =SmallestDiff(10,100)
0.00023019683430903
>

e