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

Timing problem

0 views
Skip to first unread message

Kenneth Kuan

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
Hi All,

Below is an abstraction from my program :

#include <time.h>
time_t start, stop;
float elapsed;
time(&start);
/* computation here */
time(&stop);
elapsed = stop - start;

Ok, i want to count how long it takes to compute something, the problem
is that i only get whole numbers in the variable elapsed. Eg. 15.000000
or 3.000000. I mean i want milliseconds, microseconds etc. Eg. 15.351423
or 3.978365. How do i count time more precisely?

N8TM

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
>From: Kenneth Kuan <ken...@singnet.com.sg>
>

>time(&start);
>/* computation here */
>time(&stop);

The usual portable way to do this is to use clock() in accordance with std C.
On most systems other than DOS this will give a resolution no worse than 0.010
seconds. It is not possible to infer actual resolution from the tick rate. On
W9X systems, it is not possible to separate process time from elapsed time,
although a resolution of about 6 microseconds is possible, while most other
systems give the process time.

Tim Prince
tpr...@computer.org

Kenneth Kuan

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
Sorry ppl,

Typo in 1 of the lines.
(elapsed = stop - start;) should be ==> (elapsed = difftime(stop, start);)
Anyone who can solve it? Thanks in advance.

Kenneth Kuan wrote:

> Hi All,
>
> Below is an abstraction from my program :
>
> #include <time.h>
> time_t start, stop;
> float elapsed;

> time(&start);
> /* computation here */
> time(&stop);

Jack Klein

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
On Tue, 29 Dec 1998 13:43:52 +0800, Kenneth Kuan
<ken...@singnet.com.sg> wrote:

> Hi All,
>
> Below is an abstraction from my program :
>
> #include <time.h>
> time_t start, stop;
> float elapsed;
> time(&start);
> /* computation here */
> time(&stop);
> elapsed = stop - start;
>
> Ok, i want to count how long it takes to compute something, the problem
> is that i only get whole numbers in the variable elapsed. Eg. 15.000000
> or 3.000000. I mean i want milliseconds, microseconds etc. Eg. 15.351423
> or 3.978365. How do i count time more precisely?
>


<Jack>

Standard C does not define the units of a time_t, so I hope you are
using difftime() to get the elapsed time in seconds.

The standard C answer is to use clock(), also prototyped in time.h.
It usually has better resolution than time() but almost certainly not
as good as you want.

If the point of timing is to compare two different approaches, putting
each of them in a loop and repeating them however many thousands or
millions of times it takes to get measurable and repeatable answers
will give you an indication.

There are non-standard Windows API functions which can give you higher
resolution, but you'll have to ask about them in a Win32 newsgroup or
maybe one of the Microsoft support groups in the
news:microsoft.public.vc.* family, since they are not a part of C.

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.


paolo...@my-dejanews.com

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
In article <36886C18...@singnet.com.sg>,

Kenneth Kuan <ken...@singnet.com.sg> wrote:
> Hi All,
>
> Below is an abstraction from my program :
>
> #include <time.h>
> time_t start, stop;
> float elapsed;
> time(&start);
> /* computation here */
> time(&stop);
> elapsed = stop - start;
>
> Ok, i want to count how long it takes to compute something, the problem
> is that i only get whole numbers in the variable elapsed. Eg. 15.000000
> or 3.000000. I mean i want milliseconds, microseconds etc. Eg. 15.351423
> or 3.978365. How do i count time more precisely?
>
Try with clock() and read the C-FAQ 19.37.
Ciao Paolo

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

wetboy

unread,
Dec 29, 1998, 3:00:00 AM12/29/98
to
Kenneth Kuan <ken...@singnet.com.sg> wrote:
: Hi All,

< snip >

: Ok, i want to count how long it takes to compute something, the problem


: is that i only get whole numbers in the variable elapsed. Eg. 15.000000
: or 3.000000. I mean i want milliseconds, microseconds etc. Eg. 15.351423
: or 3.978365. How do i count time more precisely?

Try the _ftime() function, which exists in many C libraries.

-- Wetboy

0 new messages