Timing an operation

Say we have written a function named foo , which takes one argument and returns a number. We want to know roughly how long this function takes on some typical parameters.

Python

One option in python is the built-in timeit library. Usage might look like this:

Using timeit

Note you pass a string containing the code you want to time the execution of. Note also that you can’t use the return value of the call to avg afterwards. The setup argument is a bit cumbersome, maybe that’s a way around that.

It seems a more common approach by Pythonista’s is to just use time :

Using time manually

This can be somewhat error-prone but generally works OK. Since it takes time before and after a line, if the line being timed is more complicated it’s not easy to time part of the line (e.g. some sub-expression of it).

We could also write our own timing function that takes a function of no arguments:

Custom timing function

Then usage would look like this:

Using time_call

Note that this allows the result of the call to foo to be captured and used. This approach is more amenable to adding timing of something inside a bigger piece of code then removing it shortly afterward.

Java

I don’t know of anything built into Java that helps with this. We can write some fairly generic timing functions:

Custom timing functions for Java

The signature taking a Supplier is for something that will return a value, and the one taking a Runnable is for something that will not. Usage looks like:

Using the custom timing functions in Java

This is pretty similar to the Python case, although more verbose due to the type system. Note however that in both Python and Java, timing a piece of code may require wrapping it in another expression so that it’s some kind of function.

Clojure’s turn

Clojure has a built-in operation called time which we can use like so:

Using Clojure time

time also returns its argument, so if the call to foo is within the context of other code (as is typical), it can be easily inserted and removed at will:

Before you say “but I have to balance the parens when removing or adding the call to time!”, good editors for Lisp’s like Clojure make that painless. Removing (time automatically removes the corresponding right parens for that expression.

How does time work without requiring its argument to implement some interface or contract? In Python, the argument could be a string as it was when using timeit , but timeit doesn’t return the result of evaluating the code; additionally, it’s not as easy to insert a timeit call since it requires making the code into a string. In both Python and Java, you can time something by accepting a function, but this requires the caller to wrap the code to be timed in a lambda expression.

The answer of course, is that Clojure time is an example of a Lisp macro rather than a function. Let’s look at the implementation of time (documentation omitted for brevity):

Implementation of time from clojure.core

time is a macro taking one argument named expr . The magic happens in the expression ret# ~expr , which executes the provided expression such as (foo 5) and assigns it to the variable ret# . The ~ operator in a macro evaluates an expression. # as in ret# is a way of ensuring a unique variable name in the scope of this macro to avoid using a variable in an enclosing scope (i.e. one defined elsewhere in the program) by mistake.

So, Clojure’s version of time takes an arbitrary code snippet — which in a Lisp-like Clojure is represented as a List surrounded by () — evaluates it, prints how long that took, and returns the result.

We can see how this macro works by using macroexpand: