News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

counting nanoseconds....

Started by dn5a, January 26, 2006, 03:01:05 AM

Previous topic - Next topic

dn5a

hey,
I'm wondering how to write a code
which would count a time to nanoseconds
can any one show me?
It's easy to write with milliseconds
with SYSTEMTIME STRUCT
but how to do til nanoseconds?
It would be a great way to use such a thing
in pseudo-random generators, together mixing
with keyboard input and mouse coordinates.
Thanks :)

BogdanOntanu

Well, conceptually speaking:

You can do microsecconds by accessing the CTC counters that run at 1,193,181Mhz in any AT motherboard.
The relevant ports are 40,41,42 and 43h. Accessing them in Windows may be complicated by the OS architecture and API

But there must be a way or an API or a driver IOCTL. My guess would be: QuerryPerformanceCounter and QueryPerformanceFrequency APIs

For reliable nanoseconds you might have to design a custom hardware counter to be added to your system
Or you can use the RDTSC instruction.

However RDTSC depends on CPU frequency and the later CPU's (mainly on laptops) will change their frequency depending on load.
As a consequence RDTSC becomes "unreliable" unless you know the current CPU frequency is fixed.

To summarize the solutions are:
1) use CTC timmer for microsecconds

2) Use/design a custom hardware timer and add that device to your system
NOTE: it might be possible that some controllers (network, labdevices) already contain such counters
but it depends on hardware and is not guaranteed to exist on all systems

3)Use RDTSC but take speciall care about CPU changing frequency
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

zooba

NtQuerySystemInformation with SystemInformationClass == SystemProcessorPerformanceInformation returns values in 1/100ths of a nano-second. Otherwise all the nanosecond timings are hidden away in multimedia libraries (ie. DirectX & Windows Media)

Use CryptGenRandom for random data. It uses about as complicated a generation process as possible :U

BogdanOntanu

Byt the LARGE (64bits) type of the IdleTime/KernelTime/UserTime structure elements
I would say that it is simply returning the accumulated values of RDTSC but in a much more complicated and slow way...

For a real nanseccond timing you need a very fast way to get the counter values...
and calling and intermediate layer of API is not going to help much...

As for "1/100th" of a nanoseccond :D let me breathe a little and take a huge laugh :))...

1.000.000 - 1Mhz - 1micro - 10 at power -6
1.000.000.000 -1GigaHertz - 1 nano is 10 at power -9

A CPU at 1Ghz will return RDTSC values in 1nanosecconds increments but is is very unlikely that any code will be able to retrieve and measure just 1ns since the code execution itself will take a  longer time to execute ....

For 1/100 of a nanosecond you will need a CPU at 100Ghz :D IMHO not yet into production --> so I guess the values returned are just bully or missleading or (in fact) reserved space for further evolution of CPU's. Make your own calculations, do not simplu believe any MSDN documentation.

And even more you will need an application able to read such counter values fast enough and make a decission of some sort based on it ... well deam on...

You might have a chnace to delay/count for about 100-400nsns on a 4Ghz CPU but to get 1/100ns is very very very unlikely....

In other MSDN places they say 100ns and this looks much realistic so maybe they made a typo mistake here.
However it is 100 times slower that what the original poster requested :D

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

elcricri

you have
Declare Function QueryPerformanceCounter Lib "Kernel32" (X As Currency) As Boolean
Declare Function QueryPerformanceFrequency Lib "Kernel32" (X As Currency) As Boolean
in visual basic
QueryPerformanceFrequency Freq
QueryPerformanceCounter Ctr1
....
QueryPerformanceCounter Ctr2
time=(ctr2-ctr1)/freq     10 at power -6

or asm
rdtsc
push eax
push edx
.....
pop ecx
pop ebx
rdtsc
edx eax - ecx ebx = number of clock