# Best way to optimize code

**URL:** <https://discourse.julialang.org/t/best-way-to-optimize-code/37980>\
**Category:** Performance\
**Tags:** question\
**Created:** [April 21, 2020, 6:10pm UTC](https://discourse.julialang.org/t/best-way-to-optimize-code/37980 "2020-04-21T18:10:00Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [April 22, 2020, 3:31pm UTC](https://discourse.julialang.org/t/best-way-to-optimize-code/37980/19 "2020-04-22T15:31:15Z")

</div>

> [@Palli](#):
>
> I looked a bit into what’s currently possible without the algorithm, at least if you can handle bias

If you are interested in previous discussions, you can have a look at [random sampling from an (abstract)array is slow · Issue #20582 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/20582) and [rand(1:10) vs Int(round(10\*rand())](https://discourse.julialang.org/t/rand-1-10-vs-int-round-10-rand/14339) (and maybe [RFC: implement a biased rand(a:b) (fix #20582) by rfourquet · Pull Request #28987 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/28987) which implements a biased `rand(a:b)`).

Also, I don’t know which version of julia you are using, but on 1.3 and more, I will repeat myself by recommending to use an explicit RNG when you benchmark, if what you want to measure is the performance of the algorithm to sample from a range, i.e. `@btime rand($rng, 1:3)` and not `@btime rand(1:3)` where `rng = MersenneTwister();`.

---

_[View the full topic](https://discourse.julialang.org/t/best-way-to-optimize-code/37980)._
