The compilation buffer prints out start and end date for my compilation. How can I make it also tell me how many minutes & seconds it spent?

I'd like something like: Compilation started at Thu Mar 16 09:10:44 [..] Compilation finished at Thu Mar 16 09:13:04 Compilation took n hours n minutes n seconds

  • 1
    Perhaps an "around" advice to the compile function? Store the current time at the start, again at the end and print out the difference to the compilation buffer. – NickD Mar 16 '17 at 22:02

Amazing nobody's answered this yet, but I can't find an answer anywhere. Here's my solution:

(make-variable-buffer-local 'my-compilation-start-time)

(add-hook 'compilation-start-hook #'my-compilation-start-hook)
(defun my-compilation-start-hook (proc) 
  (setq my-compilation-start-time (current-time)))

(add-hook 'compilation-finish-functions #'my-compilation-finish-function)
(defun my-compilation-finish-function (buf why)
  (let* ((elapsed  (time-subtract nil my-compilation-start-time))
         (msg (format "Elapsed: %s" (format-time-string "%T.%N" elapsed t))))
    (save-excursion (goto-char (point-max)) (insert msg))
    (message "Compilation %s: %s" (string-trim-right why) msg)))
| improve this answer | |
  • That works perfectly, I've wanted this for so long! Thank you @alan-conway :-) – skybert Mar 18 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.