You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is an example of how to use these functions in a program:
69
+
Here is an example of how to use these functions in a program (but frankly, use the `local-time` library instead):
67
70
68
71
~~~lisp
69
72
CL-USER> (defconstant *day-names*
@@ -106,73 +109,36 @@ Since universal times are simply numbers, they are easier and safer to manipulat
106
109
107
110
### Internal Time
108
111
109
-
Internal time is the time as measured by your Lisp environment, using your computer's clock. It differs from universal time in three important respects. First, internal time is not measured starting from a specified point in time: it could be measured from the instant you started your Lisp, from the instant you booted your machine, or from any other arbitrary time point in the past. As we will see shortly, the absolute value of an internal time is almost always meaningless; only differences between internal times are useful. The second difference is that internal time is not measured in seconds, but in a (usually smaller) unit whose value can be deduced from [`INTERNAL-TIME-UNITS-PER-SECOND`](http://www.lispworks.com/documentation/HyperSpec/Body/v_intern.htm):
112
+
Internal time is the time as measured by your Lisp environment, using your computer's clock. It differs from universal time in three important respects. First, internal time is not measured starting from a specified point in time: it could be measured from the instant you started your Lisp, from the instant you booted your machine, or from any other arbitrary time point in the past. As we will see shortly, the absolute value of an internal time is almost always meaningless; only differences between internal times are useful. The second difference is that internal time is not measured in seconds, but in a (usually smaller) unit whose value can be deduced from [`internal-time-units-per-second`](http://www.lispworks.com/documentation/HyperSpec/Body/v_intern.htm):
110
113
111
114
~~~lisp
112
115
CL-USER> internal-time-units-per-second
113
116
1000
114
117
~~~
115
118
116
-
This means that in the Lisp environment used in this example, internal time is measured in milliseconds. Finally, what is being measured by the "internal time" clock? There are actually two different internal time clocks in your Lisp: one of them measures the passage of "real" time (the same time that universal time measures, but in different units), and the other one measures the passage of CPU time, that is, the time your CPU spends doing actual computation for the current Lisp process. On most modern computers these two times will be different, since your CPU will never be entirely dedicated to your program (even on single-user machines, the CPU has to devote part of its time to processing interrupts, performing I/O, etc). The two functions used to retrieve internal times are called [`GET-INTERNAL-REAL-TIME`](http://www.lispworks.com/documentation/HyperSpec/Body/f_get_in.htm) and [`GET-INTERNAL-RUN-TIME`](http://www.lispworks.com/documentation/HyperSpec/Body/f_get__1.htm) respectively. Using them, we can solve the above problem about measuring a function's run time:
A good way to see the difference between real time and run time is to test the above code using a call such as `(SLEEP 3)`. The [`SLEEP`](http://www.lispworks.com/documentation/HyperSpec/Body/f_sleep.htm) function suspends the execution of your code for the specified number of seconds. You should therefore see a real time very close to the argument of `SLEEP` and a run time very close to zero. Let's turn the above code into a macro in order to make it more general:
132
-
133
-
~~~lisp
134
-
CL-USER> (defmacro timing (&body forms)
135
-
(let ((real1 (gensym))
136
-
(real2 (gensym))
137
-
(run1 (gensym))
138
-
(run2 (gensym))
139
-
(result (gensym)))
140
-
`(let* ((,real1 (get-internal-real-time))
141
-
(,run1 (get-internal-run-time))
142
-
(,result (progn ,@forms))
143
-
(,run2 (get-internal-run-time))
144
-
(,real2 (get-internal-real-time)))
145
-
(format *debug-io* ";;; Computation took:~%")
146
-
(format *debug-io* ";;; ~f seconds of real time~%"
;;; Computation took: 0.994 seconds of real time 0.0 seconds of run
155
-
;;; time
156
-
NIL
157
-
~~~
119
+
This means that in the Lisp environment used in this example, internal time is measured in milliseconds.
120
+
121
+
Finally, what is being measured by the "internal time" clock? There are actually two different internal time clocks in your Lisp:
122
+
123
+
- one of them measures the passage of "real" time (the same time that universal time measures, but in different units), and
124
+
- the other one measures the passage of CPU time, that is, the time your CPU spends doing actual computation for the current Lisp process.
158
125
159
-
The built-in macro [`TIME`](http://www.lispworks.com/documentation/HyperSpec/Body/m_time.htm) does roughly the same as the above macro (it executes a form and prints timing information at the end), but it also usually provides information about memory usage, time spent in garbage collection, page faults, etc. The format of the output is implementation-dependent, but in general it's pretty useful and informative. This is an example under Allegro Common Lisp 6.0: we generate a list of 100 real numbers and we measure the time it takes to sort them in ascending order.
126
+
On most modern computers these two times will be different, since your CPU will never be entirely dedicated to your program (even on single-user machines, the CPU has to devote part of its time to processing interrupts, performing I/O, etc). The two functions used to retrieve internal times are called [`get-internal-real-time`](http://www.lispworks.com/documentation/HyperSpec/Body/f_get_in.htm)and [`get-internal-run-time`](http://www.lispworks.com/documentation/HyperSpec/Body/f_get__1.htm) respectively. Using them, we can solve the above problem about measuring a function's run time, which is what the `time` built-in macro does.
160
127
161
128
~~~lisp
162
-
CL-USER> (let ((numbers (loop for i from 1 to 100 collect (random 1.0))))
163
-
(time (sort numbers #'<)))
164
-
; cpu time (non-gc) 0 msec user, 10 msec system
165
-
; cpu time (gc) 0 msec user, 0 msec system
166
-
; cpu time (total) 0 msec user, 10 msec system
167
-
; real time 9 msec
168
-
; space allocation:
169
-
; 3,586 cons cells, 11,704 other bytes, 0 static bytes
129
+
CL-USER> (time (sleep 1))
130
+
Evaluation took:
131
+
1.000 seconds of real time
132
+
0.000049 seconds of total run time (0.000044 user, 0.000005 system)
133
+
0.00% CPU
134
+
2,594,553,447 processor cycles
135
+
0 bytes consed
170
136
~~~
171
137
172
138
173
139
## The `local-time` library
174
140
175
-
The `local-time` library (available on Quicklisp) is a very handy extension to
141
+
The [local-time](https://common-lisp.net/project/local-time/) library ([GitHub](https://github.com/dlowe-net/local-time/)) is a very handy extension to
176
142
the somewhat limited functionalities as defined by the standard.
0 commit comments