lua-users home
lua-l archive

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


Diego Nehab wrote:
Hi,

Have you considered  to use an RDTSC timer for intel platforms?
The code is in some ways more portable than times().

For win32

_int64 t;

#define RDTSC_TIMER(x) {__asm rdtsc  __asm mov DWORD PTR [x],EAX
__asm  mov DWORD PTR [x+4],EDX}

 RDTSC_TIMER(t)


Or, you can use QueryPerformanceCounter, if you don't like __asm.  If
you don't need that much precision, you can also use
GetSystemTimeAsFileTime.

I have gotten recent experience with QueryPerformanceCounter(). I naively tried to get its value and divide it by the value in QueryPerformanceFrequency(), first converting both to doubles. I found that this conversion reduced the precision down to about 100ms. This is because on my shiny new 3GHz P4 machine, the frequency is 3GHz! That means the counter increments by 3 billion every second. Since it's a 64 bit counter it will still not rollover for about 200 years. But if your machine has been running for a month or so like mine was, the number gets too big to be represented very precisely by a double!

This never troubled me before I found because my older 1.7GHz P4 ran at a frequency of only 3.5MHz.

My solution was to snapshot a count value when my app started and subtract this from the current value. Now I get nice sub-microsecond accruracy. The problem there, of course is that, if my app runs for many days, the precision will slowly be reduced back to the intolerable 100ms level. I plan to solve that by periodically taking another snapshot of the counter as the initial value.

I can post my final solution when I have it, if anyone is interested...

--
chris marrin                ,""$,
chris@marrin.com          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'