Counting Words IV: Using Org tables for benchmarking

I will wrap up this series with a short post that may be of interest to org-mode users: one way of building a benchmarking table such as those you saw in part III. Here's how I did it.

First, I wrote a function that outputs elapsed time given name of function and input file:

(defun cw-bench-it (fun file &optional ddir no-file)
  "Given path of function FUN and FILE, return elapsed time.
If DDIR is non-nil, use it as ‘default-directory’.
If NO-FILE is non-nil, don't output to file. If nil, output it to a file in
subdir ./out of path of FILE.
FUN, FILE, and DDIR are passed as strings."
  (let* ((default-directory (or ddir default-directory))
         (file (if (f-exists-p file)
                   file
                 (format "%s/%s" (f-parent (f-this-file)) file)))
         (cmd `(,(read fun) ,file)))
    (format "%.3f"
            (if no-file
                (benchmark-elapse (apply #'funcall cmd))
              (let* ((outdir  (concat (f-parent file) "/../out"))
                     (outfile (concat outdir "/" (f-base file) "--" fun)))
                (mkdir outdir '-p)
                (benchmark-elapse
                  (f-write (apply #'funcall cmd) 'utf-8 outfile)))))))

Then I created here an empty org table with a formula:

|               | foo | kjv÷10 | kjv | kjv×10 |
|---------------+-----+--------+-----+--------|
| cw-count-it-1 |     |        |     |        |
| cw-count-it-2 |     |        |     |        |
| cw-count-it-3 |     |        |     |        |
| cw-count-it-4 |     |        |     |        |
| cw-count-it-5 |     |        |     |        |
#+tblfm: @2$2..@>$>='(cw-bench-it $1 (concat "in/" @1 ".txt") "~/path/to/countwords/")

A third parameter is used because the file I'm writing this on is not in the same directory of my countwords.el, so I need to let-bind default-directory to the latter for the files to be found.

A C-cC-c on the formula line fills it up. This in turn is transformed into the table you previously saw, thanks to org-export and to a bit of CSS.