exact time

H

Henning Hasemann

Hi all,
sounds trivial (I hope it is): Im searching for a way to get a
time-value thats more exact than just seconds (i.e. I need milli- or
microseconds or something similar, just at least as good as 1/100 second
should suffice) and it should work with gcc (really g++) under linux and
the MingW compliant under windows xp.

I found an easy and universal sounding way in gmtime & co, but they dont
seem to be available under my windows mingw installation.
Is gmtime standard? Is my mingw installation broken?

If it whould be too compilcated to find *one* way for both os' I whould
be happy about two #ifdef'ed blocks of code, too.

TIA
Henning
 
K

Keith Thompson

Henning Hasemann said:
sounds trivial (I hope it is): Im searching for a way to get a
time-value thats more exact than just seconds (i.e. I need milli- or
microseconds or something similar, just at least as good as 1/100
second should suffice) and it should work with gcc (really g++) under
linux and the MingW compliant under windows xp.

I found an easy and universal sounding way in gmtime & co, but they
dont seem to be available under my windows mingw installation.
Is gmtime standard? Is my mingw installation broken?

If it whould be too compilcated to find *one* way for both os' I
whould be happy about two #ifdef'ed blocks of code, too.

gmtime() is a standard C function; your mingw installation almost
certainly supports it. It may be missing some other non-standard
functions.

There is no portable way to get a timestamp to any particular
resolution; the C standard doesn't even guarantee 1-second resolution
for time().

See question 19.37 in the comp.lang.c FAQ, <http://www.c-faq.com/>.

For more information, consult the documentation for the systems you're
using and/or post to a system-specific newsgroup.

<OFF_TOPIC><HINT>gettimeofday</HINT></OFF_TOPIC>
 
R

Richard Bos

Henning Hasemann said:
sounds trivial (I hope it is): Im searching for a way to get a
time-value thats more exact than just seconds (i.e. I need milli- or
microseconds or something similar, just at least as good as 1/100 second
should suffice) and it should work with gcc (really g++) under linux and
the MingW compliant under windows xp.

I found an easy and universal sounding way in gmtime & co, but they dont
seem to be available under my windows mingw installation.
Is gmtime standard? Is my mingw installation broken?

gmtime() is ISO C. If your MingW doesn't have it, it's broken. FWIW, my
installation Dev-C++ with MingW does have it. However, the ISO C part of
gmtime() cannot possibly give you sub-second resolution, because there
is no required member in the struct tm it creates smaller than an
integral number of seconds (though it is allowed to add additional
members, but those are then non-Standard and therefore non-portable);
even a time_t is not guaranteed to have that small a resolution (though
of course it's allowed to); in fact, there is no ISO C function that
does give you this.

Richard
 
F

Flash Gordon

Henning said:
Hi all,
sounds trivial (I hope it is): Im searching for a way to get a
time-value thats more exact than just seconds (i.e. I need milli- or
microseconds or something similar, just at least as good as 1/100 second
should suffice)

Standard C provides no such mechanism.
> and it should work with gcc (really g++)

g++ is a C++ compiler, *not* a C compiler. C++ is a different language
with it's own group.
> under linux and
the MingW compliant under windows xp.

I found an easy and universal sounding way in gmtime & co, but they dont

It's hardly universal if it isn't supported everywhere!
seem to be available under my windows mingw installation.
Is gmtime standard? Is my mingw installation broken?

If it whould be too compilcated to find *one* way for both os' I whould
be happy about two #ifdef'ed blocks of code, too.

To get sub second resolution you will have to use OS specific mechanisms
which almost certainly means you will have to use conditional compilation.
 
C

Chris McDonald

Henning Hasemann said:
If it whould be too compilcated to find *one* way for both os' I whould
be happy about two #ifdef'ed blocks of code, too.

<OT>
Some code for Linux:


#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/resource.h>

static struct timeval startw, endw;
static struct rusage startt, endt;

static void time_taken(void)
{
printf("real=%ldms,",
((endw.tv_sec - startw.tv_sec)*1000 +
(endw.tv_usec - startw.tv_usec)/1000));
printf("usr=%ldms,",
((endt.ru_utime.tv_sec - startt.ru_utime.tv_sec)*1000 +
(endt.ru_utime.tv_usec - startt.ru_utime.tv_usec)/1000));
printf("sys=%ldms\n",
((endt.ru_stime.tv_sec - startt.ru_stime.tv_sec)*1000 +
(endt.ru_stime.tv_usec - startt.ru_stime.tv_usec)/1000));
}

static void do_something(void)
{
int i;

for(i=0 ; i<10000000 ; i++)
getpid();
}

int main(int argc, char *argv[])
{
gettimeofday(&startw, NULL); getrusage(RUSAGE_SELF,&startt);

do_something();

gettimeofday(&endw, NULL); getrusage(RUSAGE_SELF,&endt);

time_taken();

return(0);
}

</OT>
 
K

Keith Thompson

Chris McDonald said:
Henning Hasemann said:
If it whould be too compilcated to find *one* way for both os' I whould
be happy about two #ifdef'ed blocks of code, too.

<OT>
Some code for Linux:


#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h> [snip]
</OT>

Enclosing most of your article in <OT>...</OT> is not a license to
post blatantly system-specific code.

There are very good reasons *not* to post system-specific code in
comp.lang.c. The people who are qualified to comment on it don't
necessarily hang out here; they're in system-specific newsgroups.

As far as I can tell, the code you posted should work on any Unix-like
system, not just Linux; that means comp.unix.programmer is the correct
place to discuss it. <OT>There's at least one error in the code;
you'll see it if you compile it with warnings enabled.</OT>
 
C

Chris McDonald

Keith Thompson said:
Enclosing most of your article in <OT>...</OT> is not a license to
post blatantly system-specific code.

Agreed, and that not withstanding ........, I haven't got the breath to
start that debate again. I hope it was helpful to more that the OP.

As far as I can tell, the code you posted should work on any Unix-like
system, not just Linux; that means comp.unix.programmer is the correct
place to discuss it. <OT>There's at least one error in the code;
you'll see it if you compile it with warnings enabled.</OT>

Thanks; found the problem.
I bet a system-specific group wouldn't have highlighted that problem for me.
A beneficial side-effect of doing the wrong thing.
 
H

Henning Hasemann

Flash said:
Standard C provides no such mechanism.

What a pitty. As I notices this is sensitivefor going into off-topic,
(no personal statement regarding that atm), what whould be the right
place to get my windows-solution? (If it is allowed to ask)
g++ is a C++ compiler, *not* a C compiler. C++ is a different language
with it's own group.

Yes, I know. I Know the differences between C and C++. I read several
books about C++ and enough about C to clearly see the difference.
But the point is, that a solution in/for C will help me in 99% too as
including c libraries in c++ is not a problem and I supposed the
solution whould be something in time.h I didnt discover yet.
It's hardly universal if it isn't supported everywhere!

Well, to answer a few other posts too: Not that sure is was gmtime.
Reading Chris McDonalds posts brings me to the point my mind played
tricks on me (happening often at the moment) and it was gettimeofday()
which wasnt found.

But I now see the point I will have to use something OS specific, thanks
for that information to all. (Although Im not happy with that result)

Henning
 
F

Flash Gordon

Henning said:
What a pitty. As I notices this is sensitivefor going into off-topic,
(no personal statement regarding that atm), what whould be the right
place to get my windows-solution? (If it is allowed to ask)

Possibly microsoft.public.vc or somewhere in the
comp.os-ms-windows.programmer hierarchy. Check the FAQ for the groups first.

For information, asking if something is possible or how to do it is
acceptable if you don't know it can't be done in standard C.
Yes, I know. I Know the differences between C and C++. I read several
books about C++ and enough about C to clearly see the difference.
But the point is, that a solution in/for C will help me in 99% too as
including c libraries in c++ is not a problem and I supposed the
solution whould be something in time.h I didnt discover yet.

OK, but C++ may have solutions available for some things that C does not
and experts on comp.lang.c++ will know those parts of C that have been
incorporated in to the C++ standard.

But I now see the point I will have to use something OS specific, thanks
for that information to all. (Although Im not happy with that result)

Aiming for fully portable solutions is good, unfortunately not all
problems can be solved portably.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
K

Keith Thompson

Chris McDonald said:
Thanks; found the problem.
I bet a system-specific group wouldn't have highlighted that problem for me.
A beneficial side-effect of doing the wrong thing.

I bet it would have. I hang out in comp.unix.programmer sometimes,
and a subject of "exact time" probably would have caught my attention.
 
K

Keith Thompson

Henning Hasemann said:
What a pitty. As I notices this is sensitivefor going into off-topic,
(no personal statement regarding that atm), what whould be the right
place to get my windows-solution? (If it is allowed to ask)

First, check your system's documentation. If that fails, I'd try a
Google search. If that fails, post to a Windows-specific newsgroup;
comp.os.ms-windows.programmer.win32 is a likely choice, or perhaps
something under the microsoft.* hierarchy.
 
C

CBFalconer

Flash said:
.... snip ...

OK, but C++ may have solutions available for some things that C
does not and experts on comp.lang.c++ will know those parts of C
that have been incorporated in to the C++ standard.

Anything that is soluble in C++ is soluble in C. It may require a
tad more thinking. However the reverse does not apply.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
F

Flash Gordon

CBFalconer said:
Flash Gordon wrote:
... snip ...

Anything that is soluble in C++ is soluble in C. It may require a
tad more thinking. However the reverse does not apply.

It is still in general best to ask in a group about the language the
compiler being used implements, and since the OP was using g++ rather
than gcc that still suggests to me that comp.lang.c++ would have been a
better place to ask.

Anyway, they might one day add something to the C++ standard that cannot
be done in standard C, only C-with-extensions, such as guaranteed
sub-second timer resolution. ;-)
 
W

Walter Roberson

CBFalconer said:
Anything that is soluble in C++ is soluble in C. It may require a
tad more thinking. However the reverse does not apply.

If the reverse does not apply, then how were the first generation
C++ compilers actually C++ to C translators? ("cfront")

Though if I recall, cfront did not have exceptions.

I haven't looked at the C++ standard library in some time; possibly
there are some operations that are well defined in it that are
system dependant or system extensions in C ?
 
C

CBFalconer

Walter said:
If the reverse does not apply, then how were the first generation
C++ compilers actually C++ to C translators? ("cfront")

That was an example of using C to solve a C++ problem.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top