trivial-time
2026-11-29
trivial-time allows timing a benchmarking a piece of code portably
<title>Trivial Time</title>
<main>
<h1>Trivial Time</h1>
<p>
<strong>Portably get timing stats for a piece of code (and benchmark it too)</strong>
<p>
Trivial Time allows to <em>portably</em> get timing stats for a piece of code.
In most cases, the stats <code>trivial-time</code> provides are as rich as implementation-specific <code>time</code> stats.
The main benefits are:
<ul>
<li> The stats are exposed via <code>with-time</code> macro, so one can easily roll
their own <code>time</code> re-implementation.
<li> There’s <code>benchmark</code> macro for quick benchmarking for a piece of code.
<li> The format of <code>time</code> is prettier (at least to me)
than most implementation-specific <code>time</code> listings.
</ul>
<h2 id=getting-started>Getting Started</h2>
<p>
Clone the Git repository:
<pre lang=sh>
git clone --recursive https://github.com/aartaka/trivial-time ~/.local/share/common-lisp/source/
</pre>
<p>
And then load <code>"trivial-time"</code> in the REPL:
<pre lang=lisp>
(asdf:load-system :trivial-time)
;; or, if you use Quicklisp
(ql:quickload :trivial-time)
</pre>
<h2 id=examples>Examples</h2>
<p>
The main utility provided by Trivial Time is <code>with-time</code>.
It allows to bind a number of timing stats for a piece of code.
<pre lang=lisp>
(trivial-time:with-time (&key aborted gc-count gc allocated) ;; Binding
(lists lists-p) ;; Multiple value args
;; Form to run
(loop for i below 1000
collect (make-list i :initial-element :hello)
into lists
finally (return (values lists t)))
(unless aborted
(format t "Bytes allocated: ~a, GC ran ~d times for ~a seconds"
allocated gc-count gc)))
;; Bytes allocated: 7997952, GC ran NIL times for 0 seconds
</pre>
<p>
Use of <code>benchmark</code>:
<pre lang=lisp>
(trivial-time:benchmark (20) ;; Repeat count.
(loop for i below 1000 collect (make-list i) finally (return 1)))
;; Benchmark for 20 runs of
;; (LOOP FOR I BELOW 1000
;; COLLECT (MAKE-LIST I)
;; FINALLY (RETURN 1))
;; - MINIMUM AVERAGE MAXIMUM TOTAL
;; REAL-TIME 0.0 0.00175 0.019 0.035
;; USER-RUN-TIME 0.000668 0.0016634 0.016315 0.033268
;; SYSTEM-RUN-TIME 0.0 0.00021195 0.003794 0.004239
;; GC-RUN-TIME 0.0 0.00085 0.017 0.017
;; BYTES-ALLOCATED 7997952.0 8008154.5 8030464.0 160163090.0
</pre>
<p>
Provided <code>time</code> re-implementation (prototype, not production ready:)
<pre lang=lisp>
(trivial-time:time
(loop for i below 1000
collect (make-list i)
finally (return 1)))
;; Time spent successfully evaluating:
;; (LOOP FOR I BELOW 1000 ...)
;; Real time: 0.000000 seconds
;; Run time (system): 0.000085 seconds
;; Run time (user): 0.000857 seconds
;; CPU cycles: 2,274,058
;; GC time: 0.000000 seconds
;; Allocated: 8,024,240 bytes
</pre>
<style>
/* Light theme. */
:root {
--bg: var(--light, papayawhip);
--fg: var(--dark, black);
--accent: var(--light-accent, maroon);
--on-accent: var(--bg);
--gray: var(--neutral, darkgray);
}
* {
font-palette: ligth;
}
/* Dark theme. */
@media (prefers-color-scheme: dark) {
:root {
--bg: var(--dark, black);
--fg: var(--light, papayawhip);
--accent: var(--dark-accent, lightcoral);
--on-accent: var(--bg, black);
--gray: var(--neutral, lightgray);
}
* {
font-palette: dark;
}
}
/* Increased accessibility mode. */
@media (forced-colors: active) {
--bg: Canvas;
--fg: CanvasText;
--accent: AccentColor;
--on-accent: AccentColorText;
}
* {
box-sizing: border-box;
border-spacing: 0;
scrollbar-color: var(--accent) rgba(0, 0, 0, 0);
caret-color: var(--accent);
accent-color: var(--accent);
}
::selection, mark, ::target-text {
background-color: var(--accent);
color: var(--on-accent);
border-radius: 0.2em;
padding: 0 0.1em;
}
::marker {
color: var(--accent);
}
body {
margin: 0;
padding: 1em;
background: var(--bg);
color: var(--fg);
min-height: 100%;
font-size: max(1em, 16px, 1rem);
}
main {
padding: 1em;
}
@media (min-device-width: 500px) {
main {
margin: 0 auto;
max-width: 70em;
}
}
a {
color: inherit;
font-family: inherit;
font-style: italic;
}
a:hover {
background-color: var(--accent);
color: var(--on-accent);
}
dt {
color: var(--accent);
font-weight: bold;
}
.note {
border-radius: 0.2em;
border: 0.1em solid var(--accent);
padding: 0 1em;
margin: 1em 0;
}
pre {
overflow-x: auto;
}
h1[id]::after,
h2[id]::after,
h3[id]::after,
h4[id]::after,
h5[id]::after,
h6[id]::after {
content: " (#" attr(id) ")";
color: var(--gray);
}
</style>