From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: how to reset clock() Date: Tue, 10 Feb 1998 19:23:17 -0500 Organization: Two pounds of chaos and a pinch of salt. Lines: 36 Message-ID: <34E0EF75.138A@cs.com> References: <34E0B017 DOT 506A AT quantum DOT de> NNTP-Posting-Host: ppp224.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Tom Chojnacki wrote: > > I want to measure time intervals in which some function executes itself. > I used clock() function which returns time which elapsed since first > call to clock(). But how can I reset the clock() to measure another time > interval and not time since the first call again. > Are there any alternative time measuring functions which I could use ?. You can't reset the internal counter of clock() because clock() is a library function and its variables have scope local to it. If you are really sure you want to reset it, you could always copy the library source code and modify it appropriately. However, you are probably doing something that is not really a good idea: reading the value from clock() directly in your program. The best way to use clock() is to store a baseline value generated by a call to clock() and then calculate offsets from that base value. That way, you'll never have to worry about what clock() actually returns. The same applies to all timing functions like uclock() and gettimeofday(). For example, here's a way of timing the code in a loop: int start_clock, elapsed_clock; start_clock = clock(); /* code */ elapsed_clock = clock() - start_clock; printf( "Elapsed seconds = %.2f\n", (float) elapsed_clock / CLOCKS_PER_SEC ); -- --------------------------------------------------------------------- | John M. Aldrich | "Always listen to experts. They'll | | aka Fighteer I | tell you what can't be done, and why.| | mailto:fighteer AT cs DOT com | Then do it." | | http://www.cs.com/fighteer/| - Lazarus Long | ---------------------------------------------------------------------