TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Frank Adam
from: George White
date: 1998-06-19 09:02:02
subject: Towers of Hanoi {ansi}

Hi Frank,

You wrote to Tom Torfs:

FA>FA>>  while(difftime(start,clock()) <= 0);
FA>TT> I'm afraid I don't think it's a very good method at all:
FA>See what happens when you hang out in C.L.C. too long ? ;-)

FA>TT>        7.16.2.2  The difftime function
FA>TT>                double difftime(time_t time1, time_t time0);
FA>One could lose difftime anyway, as there is no reason to muck about with
FA>doubles, a simple subtraction in place of the function call
FA>would be sufficent. EG: while((start-clock()) <= 0);

Saves the overhead of a function call too :-).

FA>Still, what sort of magic do you expect difftime to perform ?
FA>The type of clock_t is a ulong, time_t is also a ulong,
FA>both of them are simple counters. TMBK there is no special
FA>bitfields in either of them.
FA>IMHO, difftime simply casts, then takes the difference
FA>between the two longs passed to it and returns a double as
FA>the result. There is nothing else for it to do, it should
FA>not have to do bit manipulating on the data, so whether we
FA>send it a time_t or a long or even a char shouldn't matter.
FA>AMOF, i've just sent other data types to it and it returns
FA>the correct sums every time. I strongly suspect that
FA>difftime simply does this:
FA>return (double) ((double) time1 - (double) time0));
...................^^^^^^^^^.........^^^^^^^^......^^

I've just had a look at the Borland 3.1 RTL source, and if you remove
the redundant casts and the brackets marked above, you've got their
code! You only need to cast time1, the language rules will force the
cast of time0 :-).

FA>IOW, what it doesn't know won't hurt it.  :-)

:-)

FA>TT> So it shouldn't be used for clock() times. This is also explicitly
FA>TT> mentioned in a footnote:
FA>TT>        237. In  order  to  measure the time spent in a program, the
FA>TT>            clock function should be called  at  the  start  of  the
FA>TT>            program  and  its return value subtracted from the value
FA>TT>            returned by subsequent calls.
FA>I don't see anything explicitly mentioned here regarding the use of clock()
FA>in difftime(), this is simply the *recommended* use for
FA>clock().
FA>This same thing is in the BC manuals as an example.

Bit odd really, as the two compilers I've looked at so far say clock()
measures time from program start.

FA>It's use is really up to the imagination.
FA>You could use clock() to nag the hell out of a user in a shareware program..
FA>if(ran_30_days())
FA> if(!(int)((unsigned long)(clock() / CLK_TCK) % 600))
FA> {
FA>  popup("Please register !");
FA>  waitforasec = clock();
FA>  while(clock() < waitforasec + CLK_TCK);
FA>  }

I doubt it :-(. Certainly Borland in BC 3.1 define clock() as returning
ticks since the program start, Watcom is the same. It's a program
execution timer, and theoretically measures program CPU usage, so I
suspect it has it's heritage in the code to measure CPU usage in
multiuser systems to charge users (Cs *NIX heritage showing through...).
So unless the program is run continuously for more than 30 days it won't
work.

George

 * SLMR 2.1a * Computers eliminate spare time.

--- Maximus/2 3.01
* Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)
SEEN-BY: 396/1 622/419 632/107 633/260 267 270 371 634/397 635/506 728
SEEN-BY: 670/213 218
@PATH: 440/4 255/1 252/356 140/1 270/101 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.