Skip to content

Commit e8a797e

Browse files
committed
ob-core: Display babel execution time
* lisp/ob-core.el (org-babel-execute-src-block, org-babel-format-result): Record the babel execution time, and then supplement the "Code block evaluation complete." (etc.) messages with the execution time when >0.05s.
1 parent d1c6d02 commit e8a797e

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

lisp/ob-core.el

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ guess will be made."
789789
(make-directory d 'parents)
790790
d))))
791791
(cmd (intern (concat "org-babel-execute:" lang)))
792-
result)
792+
result exec-start-time)
793793
(unless (fboundp cmd)
794794
(error "No org-babel-execute function for %s!" lang))
795795
(message "executing %s %s %s..."
@@ -804,7 +804,8 @@ guess will be made."
804804
(if name
805805
(format "(%s)" name)
806806
(format "at position %d" (nth 5 info)))))
807-
(setq result
807+
(setq exec-start-time (current-time)
808+
result
808809
(let ((r (funcall cmd body params)))
809810
(if (and (eq (cdr (assq :result-type params)) 'value)
810811
(or (member "vector" result-params)
@@ -847,7 +848,8 @@ guess will be made."
847848
(if (member "none" result-params)
848849
(message "result silenced")
849850
(org-babel-insert-result
850-
result result-params info new-hash lang)))
851+
result result-params info new-hash lang
852+
(time-subtract (current-time) exec-start-time))))
851853
(run-hooks 'org-babel-after-execute-hook)
852854
result)))))))
853855

@@ -2258,15 +2260,16 @@ If the path of the link is a file path it is expanded using
22582260
;; scalar result
22592261
(funcall echo-res result))))
22602262

2261-
(defun org-babel-insert-result (result &optional result-params info hash lang)
2263+
(defun org-babel-insert-result (result &optional result-params info hash lang exec-time)
22622264
"Insert RESULT into the current buffer.
22632265
22642266
By default RESULT is inserted after the end of the current source
22652267
block. The RESULT of an inline source block usually will be
22662268
wrapped inside a `results' macro and placed on the same line as
22672269
the inline source block. The macro is stripped upon export.
22682270
Multiline and non-scalar RESULTS from inline source blocks are
2269-
not allowed. With optional argument RESULT-PARAMS controls
2271+
not allowed. When EXEC-TIME is provided it may be included in a
2272+
generated message. With optional argument RESULT-PARAMS controls
22702273
insertion of results in the Org mode file. RESULT-PARAMS can
22712274
take the following values:
22722275
@@ -2571,11 +2574,18 @@ INFO may provide the values of these header arguments (in the
25712574
(not (and (listp result)
25722575
(member "append" result-params))))
25732576
(indent-rigidly beg end indent))
2574-
(if (null result)
2575-
(if (member "value" result-params)
2576-
(message "Code block returned no value.")
2577-
(message "Code block produced no output."))
2578-
(message "Code block evaluation complete.")))
2577+
(let ((time-info
2578+
;; Only show the time when something other than
2579+
;; 0s will be shown, i.e. check if the time is at
2580+
;; least half of the displayed precision.
2581+
(if (and exec-time (> (float-time exec-time) 0.05))
2582+
(format " (took %.1fs)" (float-time exec-time))
2583+
"")))
2584+
(if (null result)
2585+
(if (member "value" result-params)
2586+
(message "Code block returned no value%s." time-info)
2587+
(message "Code block produced no output%s." time-info))
2588+
(message "Code block evaluation complete%s." time-info))))
25792589
(set-marker end nil)
25802590
(when outside-scope (narrow-to-region visible-beg visible-end))
25812591
(set-marker visible-beg nil)

0 commit comments

Comments
 (0)