Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

measuring microseconds in C++

1 view
Skip to first unread message

Mehdi Amirijoo

unread,
Jun 8, 2001, 11:29:12 AM6/8/01
to
Hi,

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

unread,
Jun 8, 2001, 11:40:08 AM6/8/01
to
Use QueryPerformanceCounter() in VC. You can get the freqency at which
it
ticks from the QueryPerformanceFrequency() function.

Murrgon

Alexandre Brousseau

unread,
Jun 8, 2001, 12:30:32 PM6/8/01
to
> 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++??


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.


Roedy Green

unread,
Jun 8, 2001, 8:59:49 PM6/8/01
to
On Fri, 8 Jun 2001 17:29:12 +0200, "Mehdi Amirijoo"
<g-m...@ida.liu.se> wrote or quoted :

>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.

Gordon Beaton

unread,
Jun 9, 2001, 1:55:24 AM6/9/01
to
On Fri, 8 Jun 2001 17:29:12 +0200, Mehdi Amirijoo wrote:
> 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++??

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

R. Bukowski

unread,
Jun 9, 2001, 6:38:51 AM6/9/01
to
This can be helpful
http://www.naughter.com/cputicker.html

Radek

Patricia Shanahan

unread,
Jun 9, 2001, 8:23:24 AM6/9/01
to

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

Gordon Beaton

unread,
Jun 9, 2001, 8:33:12 AM6/9/01
to
On 09 Jun 2001 12:23:24 GMT, Patricia Shanahan wrote:

> Gordon Beaton wrote:
> > In any case you shouldn't expect to get much better than millisecond
> > resolution, no matter what mechanism you choose.
>
> Why?

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".

Tim Robinson

unread,
Jun 9, 2001, 8:39:59 AM6/9/01
to
"Gordon Beaton" <not.fo...@see.signature> wrote in message
news:9ft528$27f$1...@news.du.uab.ericsson.se...

> 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..."


Richard Norman

unread,
Jun 9, 2001, 10:23:37 AM6/9/01
to
"Roedy Green" <ro...@mindprod.com> wrote in message
news:s4t2itctr0ou3pqrv...@4ax.com...

> On Fri, 8 Jun 2001 17:29:12 +0200, "Mehdi Amirijoo"
> <g-m...@ida.liu.se> wrote or quoted :
>
> >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.
>

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!

Jerry Coffin

unread,
Jun 12, 2001, 12:37:25 AM6/12/01
to
In article <9ft5ju$nhf$1...@newsg4.svr.pol.co.uk>,
timothy....@ic.ac.invalid says...

[ ... ]

> 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.

Roedy Green

unread,
Jun 12, 2001, 9:59:27 PM6/12/01
to
On Mon, 11 Jun 2001 22:37:25 -0600, Jerry Coffin <jco...@taeus.com>
wrote or quoted :

>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

--

Neil A. Van Note

unread,
Jun 21, 2001, 11:22:33 PM6/21/01
to
Mehdi,

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...

Jon Skeet

unread,
Jun 22, 2001, 3:19:00 AM6/22/01
to
Neil A. Van Note <van...@netaxs.com> wrote:
> System.currentTimeMillis() doesn't work for you?

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

Neil A. Van Note

unread,
Jun 22, 2001, 10:20:52 AM6/22/01
to
You are certainly correct... :-) My Bad...

Best Regards,

Neil A. Van Note

"Jon Skeet" <sk...@pobox.com> wrote in message
news:MPG.159d01c3d...@mrmog.peramon.com...

Dale E. Martin

unread,
Jun 22, 2001, 10:23:49 AM6/22/01
to
"Neil A. Van Note" <van...@netaxs.com> writes:

> > 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

Ross Ridge

unread,
Jun 23, 2001, 3:33:32 PM6/23/01
to

"Dale E. Martin" <dma...@cliftonlabs.com> wrote:
> In Linux, you can use the Pentium TSC register...

Linux programmers are now assuming all the world is a Pentium?

Ross Ridge

Dale E. Martin

unread,
Jun 25, 2001, 10:41:33 AM6/25/01
to
"Ross Ridge" <rri...@csclub.uwaterloo.ca> writes:

> > 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,

Martin

unread,
Jun 25, 2001, 6:14:05 PM6/25/01
to
dma...@cliftonlabs.com (Dale E. Martin) wrote:


>> > 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

0 new messages