Microsecond timing in Julia

I’d like to take stopwatch time measurements accurate to the microsecond. Is there another way to do this besides using @elapsed? I’d ideally like to avoid encapsulating my timed piece of code in a macro. Thanks!

You can do start_time = time_ns(), then run the code, then end_time = time_ns(), and elapsed = (end_time - start_time)*1e-3 (to get it in microseconds instead of nanoseconds).

1 Like

Thanks!

1 Like

Although that’s exactly what the macro does.

4 Likes

True. I actually had a strange situation today where I needed that and I don’t think the macro would work: post processing Literate.jl files to add code with timing info, so that when Documenter later executes the code in makedocs it prints the timing info, partly so Travis doesn’t time out: https://github.com/JuliaOpt/Convex.jl/blob/4c6293d146430935ddf8d1a45aab48d8c28b64ea/docs/make.jl#L104. There @time wouldn’t really work since the code being run lives in separate @example blocks in a markdown file.