# Microsecond timing in Julia

**URL:** <https://discourse.julialang.org/t/microsecond-timing-in-julia/30877>\
**Category:** General Usage\
**Created:** [November 9, 2019, 1:24am UTC](https://discourse.julialang.org/t/microsecond-timing-in-julia/30877 "2019-11-09T01:24:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fleecy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fleecy/32/11210_2.png) [@fleecy](https://discourse.julialang.org/u/fleecy)\
**Post date:** [November 9, 2019, 1:24am UTC](https://discourse.julialang.org/t/microsecond-timing-in-julia/30877/1 "2019-11-09T01:24:55Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 9, 2019, 1:28am UTC](https://discourse.julialang.org/t/microsecond-timing-in-julia/30877/2 "2019-11-09T01:28:09Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![fleecy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fleecy/32/11210_2.png) [@fleecy](https://discourse.julialang.org/u/fleecy)\
**Post date:** [November 9, 2019, 1:36am UTC](https://discourse.julialang.org/t/microsecond-timing-in-julia/30877/3 "2019-11-09T01:36:48Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 9, 2019, 2:28am UTC](https://discourse.julialang.org/t/microsecond-timing-in-julia/30877/4 "2019-11-09T02:28:07Z")

</div>

Although that’s exactly what the macro does.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 9, 2019, 3:08am UTC](https://discourse.julialang.org/t/microsecond-timing-in-julia/30877/5 "2019-11-09T03:08:03Z")

</div>

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](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.
