timing.html
来自「Data Structure Ebook」· HTML 代码 · 共 78 行
HTML
78 行
<HTML><HEAD>
<TITLE>Data Structures and Algorithms - Timing Programs</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>Data Structures and Algorithms</H1>
<HR>
<H3>Timing a function</H3>
Most systems seem to have implementations of the ANSI C
routine <TT>clock()</TT>.
<P>
You can find its specifications with the
<BLOCKQUOTE>
<TT>man 3 clock</TT>
</BLOCKQUOTE>
command.
<P>
Each call of <TT>clock()</TT> returns the time in
<B>ticks</B> since the last call.
So, to time a function, simply call the function to be
timed in between two <TT>clock()</TT> calls:
<FONT COLOR=green><PRE>
long t;
t = clock();
/* Call function to be timed */
x = f( ..... );
/* Calculate time since previous clock call */
t = clock() - t;
/* Convert to seconds */
printf("CPU time %g\n", (double)t/CLOCKS_PER_SEC );
</PRE></FONT>
A good <B>tool-building approach</B> would have you build another
(trivial) function, say,
<PRE>
double TimeUsed();
</PRE>
which returned the time difference in
seconds and prevented your program
from needing to worry abnut the conversion from ticks to seconds.
For an example of a simple function which can readily be included
in your program, see
<A HREF="javascript:if(confirm('http://www.ee.uwa.edu.au/~plsd210/ds/source/timer.h \n\nThis file was not retrieved by Teleport Pro, because it is linked too far away from its Starting Address. If you increase the in-domain depth setting for the Starting Address, this file will be queued for retrieval. \n\nDo you want to open it from the server?'))window.location='http://www.ee.uwa.edu.au/~plsd210/ds/source/timer.h'" tppabs="http://www.ee.uwa.edu.au/~plsd210/ds/source/timer.h" TARGET=timer>timer.h</A> and
<A HREF="javascript:if(confirm('http://www.ee.uwa.edu.au/~plsd210/ds/source/timer.c \n\nThis file was not retrieved by Teleport Pro, because it is linked too far away from its Starting Address. If you increase the in-domain depth setting for the Starting Address, this file will be queued for retrieval. \n\nDo you want to open it from the server?'))window.location='http://www.ee.uwa.edu.au/~plsd210/ds/source/timer.c'" tppabs="http://www.ee.uwa.edu.au/~plsd210/ds/source/timer.c" TARGET=timer>timer.c</A>.
<P>
However, be careful to note that the <B>minimum resolution</B> of the
<TT>clock</TT> function will almost invariably be more than 1
tick. On the SGI machines, it's actually 10ms.
This means that you have to ensure that your test function
runs for <I>much longer</I> than 10ms to get accurate times.
Thus you will usually have to call the function under test
quite a few times in order to find an accurate time:
<FONT COLOR=green><PRE>
long t;
double tN;
t = clock();
/* Call function to be timed N times */
for(i=0;i<N;i++) {
x = f( ..... );
}
/* Calculate time since previous clock call */
tN = clock() - t;
/* Convert to seconds */
tN = tN/CLOCKS_PER_SEC;
/* Calculate the average */
printf("CPU time %g\n", tN/N );
</PRE></FONT>
You will need to determine a suitable value of N by experimentation ..
it will obviously vary with the complexity of the function being
tested!
<P>
<HR>
<A HREF="ds_ToC.html" tppabs="http://www.ee.uwa.edu.au/~plsd210/ds/ds_ToC.html">Table of Contents</A><BR>
<SMALL>
© <A HREF=mailto:morris@ee.uwa.edu.au>John Morris</A>, 1998
</SMALL>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?