I'm trying to measure microseconds in Java by calling C++ programs (JNI).
Does anybody know what functions in VC++ measure microseconds or in GNU
C++??
Thanks, Mehdi
Murrgon
Here's the code I use to accomodate my timing needs :
double HFClock(void) {
// Query clock frequency
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
// Query current clock tick
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
// Convert to milliseconds
return (double)now.QuadPart / (double)freq.QuadPart * 1000.0;
}
To use it:
double tm= HFClock();
// do stuff you want to time
tm= HFClock() - tm;
// tm is now the time elapsed in milliseconds, with fractions being
fractions of a millisecond.
I don't know how precise it is exactly though...
Alex.
>Does anybody know what functions in VC++ measure microseconds or in GNU
>C++??
For that sort of level of code you want to look at using ASM and
getting at the special Pentium cycle counting registers.
I may have posted something about them under Time in the Java
glossary.
For more detail, please look up the key words mentioned in this post in
the Java Glossary at: http://mindprod.com/gloss.html
If you don't see what you were looking for, complain!
or send your contribution for the glossary.
--
Roedy Green, Canadian Mind Products
Custom computer programming since 1963. Ready to take on new work.
C library function gettimeofday() returns a time consisting of seconds
and micoseconds. It doesn't necessarily give you microsecond
resolution, however.
On some platforms (specifically Solaris), gethrtime() returns the time
in nanoseconds, but with less than nanosecond resolution. There may be
something similar on Windows, I don't know.
In any case you shouldn't expect to get much better than millisecond
resolution, no matter what mechanism you choose.
/gordon
--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
ericsson research
stockholm, sweden
Gordon Beaton wrote:
...
> On some platforms (specifically Solaris), gethrtime() returns the time
> in nanoseconds, but with less than nanosecond resolution. There may be
> something similar on Windows, I don't know.
>
> In any case you shouldn't expect to get much better than millisecond
> resolution, no matter what mechanism you choose.
Why?
Patricia
Only based on comments that I've read here (c.l.j.p), that the system
clock on Windows seems to have 55 ms resolution. Maybe it's better
than that, but I don't use Windows and don't know.
Someone else replied with some information about using special pentium
timing registers, and that seems like a good way to get better
resolution on that platform, so perhaps I should have said "using
available system or library calls".
Win32 has the QueryPerformanceCounter() and QueryPerformanceFrequency()
functions. On x86 NT, these map to either the Pentium RDTSC instruction or
the high-frequency (~2MHz?) motherboard counter.
--
Tim Robinson <timothy.robinson at ic dot ac dot uk>
"We waste, we destroy, and we cling like savages to our superstitions. We
give power to leaders of state and church as prejudiced and small-minded as
ourselves, who squander our resources on instruments of destruction, while
millions continue to suffer and go hungry, condemned forever..."
Others have referred you to QueryPerformanceCounter. This does have
microsecond resolution. However, on any multitasking operating system,
you have to be prepared for task switching to occur. So your task can
be switched out of operation sometime between your call to the timer
and your analysis of the response. That means the timer will give you
a very precise measurement of an instant of time. However just when
that time measurement occurred may have an uncertainty of dozens
of milliseconds!
[ ... ]
> Win32 has the QueryPerformanceCounter() and QueryPerformanceFrequency()
> functions. On x86 NT, these map to either the Pentium RDTSC instruction or
> the high-frequency (~2MHz?) motherboard counter.
1.024 MHz, FWIW.
--
Later,
Jerry.
The Universe is a figment of its own imagination.
>The Universe is a figment of its own imagination.
see http://mindprod.com/hallucination.html where I expound on that
theme.
For more detail, please look up the key words mentioned in this post in
the Java Glossary at: http://mindprod.com/gloss.html
--
System.currentTimeMillis() doesn't work for you?
You didn't explain what you are trying to achieve
exactly, So I don't know if you are going to do the
entire timing in the C++ code.
If not and it has to be very accurate, you will also
have to take into account the time it takes for the round
trip out of the JVM, through JNI, into your native code and
back again.
Just food for thought...
Best Regards,
Neil A. Van Note
"Mehdi Amirijoo" <g-m...@ida.liu.se> wrote in message
news:9fqqv6$naj$1...@curofix.ida.liu.se...
Well it's not going to measure microseconds, certainly...
--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Best Regards,
Neil A. Van Note
"Jon Skeet" <sk...@pobox.com> wrote in message
news:MPG.159d01c3d...@mrmog.peramon.com...
> > I'm trying to measure microseconds in Java by calling C++ programs (JNI).
> >
> > Does anybody know what functions in VC++ measure microseconds or in GNU
> > C++??
Hi. High resolution timing varies from platform to platform - there's
currently no portable way to do it. I have no idea how to in Windows,
although I'm sure you can. In Solaris, check out "gethrtime". In Linux,
you can use the Pentium TSC register with the following code snippet:
u64
PentiumTSC::rdtsc(){
u64 clock;
__asm__ __volatile__("rdtsc" : "=A" (clock)); return clock;
}
I've got a stopwatch class based on this call that works in Linux if you're
interested.
Later,
Dale
--
Dale E. Martin, Clifton Labs, Inc.
Senior Computer Engineer
dma...@cliftonlabs.com
http://www.cliftonlabs.com
pgp key available
Linux programmers are now assuming all the world is a Pentium?
Ross Ridge
> > In Linux, you can use the Pentium TSC register...
>
> Linux programmers are now assuming all the world is a Pentium?
My mistake. On Intel Linux you can use the Pentium TSC counter. On other
platforms, you'll need to use some other mechanism that is possibly
architecture specific. I don't know of a way to do this portably across
architectures in Linux (which doesn't mean one doesn't exist.)
Thanks,
>> > Does anybody know what functions in VC++ measure microseconds or in
>> > GNU C++??
>
> Hi. High resolution timing varies from platform to platform - there's
> currently no portable way to do it. I have no idea how to in Windows,
> although I'm sure you can. In Solaris, check out "gethrtime". In
> Linux, you can use the Pentium TSC register with the following code
> snippet: u64
> PentiumTSC::rdtsc(){
> u64 clock;
> __asm__ __volatile__("rdtsc" : "=A" (clock)); return clock;
> }
On Windows systems you can use QueryPerformanceCounter and
QueryPerformanceFrequency:
The QueryPerformanceCounter function retrieves the current value of the
high-resolution performance counter, if one exists.
BOOL QueryPerformanceCounter(
LARGE_INTEGER *lpPerformanceCount // counter value
);
Parameters
lpPerformanceCount
[out] Pointer to a variable that receives the current performance-counter
value, in counts. If the installed hardware does not support a high-
resolution performance counter, this parameter can be zero.
Return Values
If the installed hardware supports a high-resolution performance counter,
the return value is nonzero.
If the function fails, the return value is zero. To get extended error
information, call GetLastError. For example, if the installed hardware does
not support a high-resolution performance counter, the function fails.
HTH
Martin