🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: nl.comp.programmeren
Article #645

Re: clock() c++

#645
From: Luc Kumps
Date: Mon, 14 May 2001 08:59
141 lines
5915 bytes
"Duco Fijma" <duco@lorentz.xs4all.nl> wrote in message
news:9do3a9$7oe$2@news1.xs4all.nl...
>
> Luc Kumps <NOkumpsSPAM@pandora.be> wrote in message
> news:WgLL6.78460$ii.10054404@afrodite.telenet-ops.be...
> >
> > "Duco Fijma" <duco@lorentz.xs4all.nl> wrote in message
> ...
> > > Hou er rekening mee dat "clock()" iets zegt over de CPU tijd van een
> > > process. Dat *kan* nuttige informatie zijn. Als je echter op zoek bent
> > naar
> > > een "real time" getal, kun je beter de functie time() en zijn
vriendjes
> > > gebruiken.
> >
> > Volgens mij doet is dit NIET correct. Wanneer men een Sleep(1000) zet
> tussen
> > twee clock() calls, dan zal clock() 1000 milliseconden verder zitten.
> > clock() doet hetzelfde als time(), alleen veel nauwkeuriger (want time()
> is
> > maar nauwkeurig tot op 1 seconde).
>
> Zowel de specificatie van C als de beschrijving van clock() onder Unix
(man
> 3 clock) zeggen zoiets als "clock() returns the processor (CPU) time used
by
> the program ...". De documentatie bij Visual C++ onder Windows zegt :
> "clock, Calculates the processor time used by the calling process.".

Ja, ik weet wat de documentatie zegt. Ik had ze voor mij liggen toen ik mijn
boodschap schreef. Voor ik het stuk over Unix (zie onder) schreef, had ik
beter naar "Compatibility" gekeken, dan had ik daar "ANSI" zien staan ;-)

Ik heb dit onnozel programmaatje gemaakt, gecompileerd met VC++ 6.0 SP4,
laten lopen op NT 4.0 SP6a:

#include <windows.h>
#include <stdio.h>
#include <time.h>
void main(void)
{
double begin,einde;
double duur;
begin = clock();
printf("%f\n", begin);

Sleep(1000);
einde = clock();
printf("%f\n", einde);

duur =  ((double) (einde - begin)) / CLOCKS_PER_SEC;
printf("%f\n", duur);
}

Dit is de output:
0.000000
1000.000000
1.000000

De documentatie waarover je spreekt zegt "clock returns the number of clock
ticks of elapsed processor time. The returned value is the product of the
amount of time that has elapsed since the start of a process and the value
of the CLOCKS_PER_SEC constant".
Toen ik dat las dacht ik ook dat het om CPU usage ging. Bovenstaand
programma toont echter dat het (in deze implementatie) om elapsed time gaat
(zoals bij time())

Tot overmaat van ramp geeft Microsoft een voorbeeld dat volledig in de
richting van "processor time" wijst, met CPU-verslindende loops!

Nu is het natuurlijk mogelijk (en m.i. zelfs waarschijnlijk) dat de MS
implementatie verkeerd is.
Wanneer ik http://theoryx5.uwinnipeg.ca/gnu/glibc/libc_295.html lees, dan is
het duidelijk dat clock() op CPU usage slaat.
Ik zal het 1 dezer dagen eens proberen op ons HP-UX systeem...

>
> > Het voordeel van time() is dan weer dat het ok op andere platformen
werkt
> > (bv Unix), maar dat is hier misschien niet belangrijk.
>
> Nou, je moet wel een heel raar platform hebben wil "clock()" niet werken.
De
> functie is gewoon onderdeel van de C standaard.

Je hebt gelijk. Het staat in de beschrijving van de standard C library.

Als toemaatje, hieronder de source code van de Microsoft implementatie...
Ik zou wel eens graag de GNU source code zien voor deze functie

Luc


static unsigned __int64 start_tics;

/***
*clock_t clock() - Return the processor time used by this process.
*
*Purpose:
*       This routine calculates how much time the calling process
*       has used.  At startup time, startup calls __inittime which stores
*       the initial time.  The clock routine calculates the difference
*       between the current time and the initial time.
*
*       Clock must reference _cinitime so that _cinitim.asm gets linked in.
*       That routine, in turn, puts __inittime in the startup initialization
*       routine table.
*
*Entry:
*       No parameters.
*       itime is a static structure of type timeb.
*
*Exit:
*       If successful, clock returns the number of CLK_TCKs (milliseconds)
*       that have elapsed.  If unsuccessful, clock returns -1.
*
*Exceptions:
*       None.
*
****************************************************************************
***/

clock_t __cdecl clock (
        void
        )
{
        unsigned __int64 current_tics;
        FILETIME ct;

        GetSystemTimeAsFileTime( &ct );

        current_tics = (unsigned __int64)ct.dwLowDateTime +
                       (((unsigned __int64)ct.dwHighDateTime) << 32);

        /* calculate the elapsed number of 100 nanosecond units */
        current_tics -= start_tics;

        /* return number of elapsed milliseconds */
        return (clock_t)(current_tics / 10000);
}
Technical Details
Message-ID: <58NL6.78553$ii.10067486@afrodite.telenet-ops.be>
Path: arc1-ca.pugleaf.net!archiver1.sj.google.com!newsfeed.google.com!sn-xit-03!supernews.com!nntp.cs.ubc.ca!freenix!fr.usenet-edu.net!usenet-edu.net!newsfeeds.belnet.be!news.belnet.be!afrodite.telenet-ops.be!not-for-mail
References: <eqiL6.76139$ii.9609735@afrodite.telenet-ops.be> <9dms17$sqo$1@news1.xs4all.nl> <WgLL6.78460$ii.10054404@afrodite.telenet-ops.be> <9do3a9$7oe$2@news1.xs4all.nl>

Show Full Headers
Complete Article Headers
X-Google-Language: DUTCH,ASCII-7-bit
X-Google-Thread: 107324,a047acd46d94d01f
X-Google-Attributes: gid107324,public
X-Google-Thread: fcea4,a047acd46d94d01f
X-Google-Attributes: gidfcea4,public
X-Google-ArrivalTime: 2001-05-14 02:00:11 PST
Path: arc1-ca.pugleaf.net!archiver1.sj.google.com!newsfeed.google.com!sn-xit-03!supernews.com!nntp.cs.ubc.ca!freenix!fr.usenet-edu.net!usenet-edu.net!newsfeeds.belnet.be!news.belnet.be!afrodite.telenet-ops.be!not-for-mail
From: "Luc Kumps" <NOkumpsSPAM@pandora.be>
Newsgroups: nl.comp.programmeren,be.comp.programming
References: <eqiL6.76139$ii.9609735@afrodite.telenet-ops.be> <9dms17$sqo$1@news1.xs4all.nl> <WgLL6.78460$ii.10054404@afrodite.telenet-ops.be> <9do3a9$7oe$2@news1.xs4all.nl>
Subject: Re: clock() c++
Lines: 145
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
Message-ID: <58NL6.78553$ii.10067486@afrodite.telenet-ops.be>
Date: Mon, 14 May 2001 08:59:45 GMT
NNTP-Posting-Host: 213.118.64.191
X-Complaints-To: abuse@pandora.be
X-Trace: afrodite.telenet-ops.be 989830785 213.118.64.191 (Mon, 14 May 2001 10:59:45 MET DST)
NNTP-Posting-Date: Mon, 14 May 2001 10:59:45 MET DST
Organization: Telenet Internet