performancetimejuliacpu-time

Measuring elapsed CPU time in Julia


Many scientific computing languages make a distinction between absolute time (wall clock) and CPU time (processor cycles). For example, in Matlab we have:

>> tic; pause(1); toc
Elapsed time is 1.009068 seconds.

>> start = cputime; pause(1); elapsed = cputime - start
elapsed =
        0

and in Mathematica we have:

>>In[1]:= AbsoluteTiming[Pause[1]]
>>Out[1]= {1.0010572, Null}

>>In[2]:= Timing[Pause[1]]
>>Out[2]= {0., Null}

This distinction is useful when benchmarking code run on computation servers, where there may be high variance in the absolute timing results depending on what other processes are running concurrently.

The Julia standard library provides support for timing of expressions through tic(), toc(), @time and a few other functions/macros all based on time_ns(), a function that measures absolute time.

>>julia> @time sleep(1)
elapsed time: 1.017056895 seconds (135788 bytes allocated)

My question: Is there a simple way to get the elapsed CPU time for an expression evaluation in Julia?

(Side note: doing some digging, it appears that Julia timing is based on the uv_hrtime() function from libuv. It seems to me that using uv_getrusage from the same library might give a way to access elapsed CPU time in Julia, but I'm no expert. Has anybody tried using anything like this?)


Solution

  • I couldn't find any existing solutions, so I've put together a package with some simple CPU timing functionality here: https://github.com/schmrlng/CPUTime.jl. The package is completely untested on parallel code and may have other bugs, but if anybody else would like to try it out calling

    >> Pkg.clone("https://github.com/schmrlng/CPUTime.jl.git")
    

    from the julia> prompt should install the package.