On Friday, October 16, 2015 at 7:08:28 AM UTC-4, Bartc wrote:
> On 15/10/2015 19:49, Rick C. Hodgin wrote:
> > On Thursday, October 15, 2015 at 12:14:06 PM UTC-4, Bartc wrote:
> >> This reads the time stamp counter.
> >>
> >> But why is it so slow?
>
> > What machine is it being run on? It may not exist and is being trapped
> > to a #UD undefined opcode exception, and an OS handler is simulating
> > the value.
>
> Your link says it's been present since the Pentium. I'm running on some
> AMD device; not sure which, but it's not a 486!
:-)
Hey, Bart. I know you. I know how much you hate bloated tools and
wasted hard disk space. I had to be sure you weren't running MS-DOS
6.22 on a 486, right?
:-)
> > There is also advice that you use it only when it's guaranteed your
> > thread has affinity for a single core:
> >
> >
https://en.wikipedia.org/wiki/Time_Stamp_Counter
> >
> > And there's a warning for Windows users:
> >
> > "Under Windows platforms, Microsoft strongly discourages using
> > the TSC for high-resolution timing for exactly these reasons,
> > providing instead the Windows APIs QueryPerformanceCounter and
> > QueryPerformanceFrequency."
>
> Yes, I get that there might be problems in using it as an accurate
> timer. But I only need a rough idea of elapsed time.
Yup. And there's a Win32 function which allows you set thread affinity
so you're always running on the same core:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686247%28v=vs.85%29.aspx
> (I've just discovered that if you do Windows programming, then if you
> don't check the message queue for five seconds, your windows get cleared
> and marked 'unresponsive'. So if busy executing byte-code for example,
> then every N byte-codes it might be necessary to check the elapsed time
> so that when 1 second has passed, I can call PeekMessage to reset the
> time-out.
A common thing to do is to run the other code you have in a separate
thread, and keep your main message queue running in the startup
thread. Note also, however, that Windows will only dispatch messages
to threads which inquire on HWND values from the same thread they
were created, unless you re-direct them with SetWindowLong() and
GWL_WNDPROC.
> Using GetTickCount is one slow way of doing that, compared with less
> than 10ns per byte-code, I expected rdtsc to super-fast in comparison.)
In my prior experience (mostly 2011 and earlier) it was very fast.
I'm wondering why it's changed so drastically. Seems silly. I would
think there would just need to be a simple synchronization as to clock
cycles, and that's just a constantly incrementing count. Perhaps it
hints at the fact that the internals of modern CPUs are moving away
from synchronous, and becoming asynchronous. Even so, I don't see how
they couldn't not keep a simple incrementing register per tick in sync
across all cores. Surely the clock generator could always send out a
high speed signal to that unit, even if the clock is being slowed down
to conserve power / heat.