emacs.d/clones/colinallen.dnsalias.org/lp/node39.html

49 lines
2.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Originally converted to HTML using LaTeX2HTML 95 (Thu Jan 19 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE> Timing Function Calls</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Timing Function Calls">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node40.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node29.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node38.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node40.html"> Exercises</A>
<B>Up:</B>
<A HREF="node29.html"> Recursion and Iteration</A>
<B> Previous:</B>
<A HREF="node38.html"> Tail Recursion</A>
<BR> <HR> <P>
<H1> Timing Function Calls</H1>
<P>
The Lisp interpreter provides a interesting feature for timing the evaluation of a function call. Any expression can be timed using <code>(time &lt;exp&gt; )</code>. For example, here's time applied to the recursive and iterative versions of power as evaluated by the interpreter:
<BLOCKQUOTE>
<PRE>&gt; (time (power 2 100))
real time : 0.200 secs ;; your output will vary
run time : 0.133 secs
1267650600228229401496703205376
&gt; (time (power-i 2 100))
real time : 0.200 secs
run time : 0.083 secs
1267650600228229401496703205376
</PRE>
</BLOCKQUOTE>
To assess the relative speeds of these two functions, run time (i.e. the amount of time required by the computer's central processing unit to perform the calculation) is the key figure. In this example, power-i uses about 2/3 as much cpu time as power.
<P>
Real time refers to the time it actually took the computer to return the answer to the screen. In this example, there is no difference in real time between the two functions. Run time and real time can differ because computers running multitasking operating systems, such as UNIX(tm), switch cpu time between different processes to give the appearance of running them simultaneously. If there are a lot of other processes running on the computer, the run time for the Lisp function is unchanged, but real time increases.
<P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>