Is there an easy way to time how long it takes to execute source code blocks in org-mode (maybe with header arguments or similar), if so, how? Or do I have to implement it in the code itself?

I'm using python3 btw.

  • Not that I know of.
    – NickD
    Oct 13 '20 at 19:49

This can be handled by advising the code that handles source code block execution in Org Mode. The function in question is org-babel-execute-src-block - it runs a hook after it executes, but not before, so the advice feature is required - and probably preferred for this use.

(defun gjg/time-call (time-call &rest args)
  (message "Ohai %s" args)
  (let ((start-time (float-time)))
    (apply time-call args)
    (message "Function call took %f seconds" (- (float-time) start-time)))

(advice-add 'org-babel-execute-src-block :around #'gjg/time-call)

You will now get a message with the elapsed time for the code block execution printed in the *Messages* buffer.

To remove the advice, execute

(advice-remove 'org-babel-execute-src-block #'gjg/time-call)

There is a mistake: the result of function "time-call" must be passed forward.

(defun gjg/time-call (time-call &rest args)
  (message "Ohai %s" args)
  (let ((start-time (float-time))
        (result (apply time-call args)))
    (message "Function call took %f seconds" (- (float-time) start-time))

(advice-add 'org-babel-execute-src-block :around #'gjg/time-call)

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.