Software Archive
Read-only legacy content
17061 Discussions

A millisecond is too fat

lottocheatah
Beginner
411 Views
Today's computers are reaching upwards of 6Ghz speeds, but the system clock hasn't changed muchsince the first 8086. I'm a software developer. I'm hitting this problem. The smallest unit of time I have to work with is the tick of the system clock, ie; 1 millisecond. If I run a routine in a loop that starts with a random number, that loop can start over before 1 millisecond ticks to the next.
There's this thing with computers that if you seed the random number generator with the same number. I run an apwith a routinethat starts with a random number (which gets generated bythe clock)and performs over 1,000 calculations based on random sampling. I get two, three, even up to five results repeating before new results show.. then the same repeating again.
Amillisecond is too fat. We need a new clock with skinnier time slices.
0 Kudos
3 Replies
Intel_Software_Netw1
411 Views

We'll forward your feedback to our engineering contacts and let you know how they respond.

Regards,

Lexi S.

IntelSoftware NetworkSupport

http://www.intel.com/software

Contact us

0 Kudos
Intel_Software_Netw1
411 Views

Our engineering contacts responded:

You can use RDTSC, which is the processor counter. It is updated every clock cycle. It is not a serializing instruction, so if you want a time stamp after all the work is done, also use a serializing instruction like CPUID.

For the case below you do not need it, but you will if you are timing a piece of code.

Also, this will not work well on laptops running on battery.


Here is some example code for precise counters. Just include this .h file and use macros to start, stop and get time.

#define _USE_COUNTERS_

#ifndef _PERF_TIMERS_

#define _PERF_TIMERS_

#ifdef _USE_COUNTERS_

#define INIT_COUNTER

unsigned __int64 start, end;

void *pStart = &start;

void *pEnd = &end;

char pClockTickBuf[100];

#define START_COUNTER

{_asm mov eax, 0

_asm cpuid

_asm rdtsc

_asm mov ebx, pStart

_asm mov dword ptr[ebx], eax

_asm mov dword ptr[ebx+4], edx

_asm mov eax, 0

_asm cpuid}

#define STOP_COUNTER

{_asm mov eax, 0

_asm cpuid

_asm rdtsc

_asm mov ebx, pEnd

_asm mov dword ptr[ebx], eax

_asm mov dword ptr[ebx+4], edx

_asm mov eax, 0

_asm cpuid}

#define GET_COUNT(buf)

{_ui64toa(end-start,buf,10);}

#define GET_COUNT_MS(buf)

{_ui64toa((end-start)/3000000,buf,10);}

#else

#define INIT_COUNTER

#define START_COUNTER

#define STOP_COUNTER

#define GET_COUNT(buf)

#endif // _USE_COUNTERS_

#endif //_PERF_TIMERS_


We also found some further explanation on this third-party site:
http://www.codeguru.com/Cpp/misc/misc/timers/article.php/c3895/

Please note that these links will take you to external web sites. Intel is not responsible for content or availability of these sites.

Regards,

Lexi S.

IntelSoftware NetworkSupport

http://www.intel.com/software

Contact us

0 Kudos
blogma-ster
Beginner
411 Views
If you are running windows, you can always get a real-time extension to windows to give you more of a RTOS environment.
You'll be able to get 100us timer resolution.
0 Kudos
Reply