# Convert Float to Integer

**URL:** <https://discourse.julialang.org/t/convert-float-to-integer/35948>\
**Category:** General Usage\
**Created:** [March 13, 2020, 9:53pm UTC](https://discourse.julialang.org/t/convert-float-to-integer/35948 "2020-03-13T21:53:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diger/32/36786_2.png) [@Diger](https://discourse.julialang.org/u/Diger)\
**Post date:** [March 13, 2020, 9:53pm UTC](https://discourse.julialang.org/t/convert-float-to-integer/35948/1 "2020-03-13T21:53:46Z")

</div>

I was wondering why converting floats to integers takes so much time? When I run this loop below it takes about 14ms. When I remove the Int() it takes only 1.9ns if I’m not mistaken. Any idea how to improve the speed?

using BenchmarkTools  
@btime for x=1:1000000  
y=Int(floor((x-1/x)/sqrt(3)))  
end

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 13, 2020, 10:02pm UTC](https://discourse.julialang.org/t/convert-float-to-integer/35948/3 "2020-03-13T22:02:20Z")

</div>

Rule number 1 of benchmarking is to never do so in the global scope. Rule number 2 is to benchmark something that makes sure the compiler doesn’t cheat. My guess is that in the 1st case, the compiler is able to just perform `y=Int(floor((1000000-1/1000000)/sqrt(3)))`

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 13, 2020, 10:03pm UTC](https://discourse.julialang.org/t/convert-float-to-integer/35948/4 "2020-03-13T22:03:31Z")

</div>

and note that integer magnitudes \> `maxintfloat(Float64)` cannot be assured to convert correctly.

---

<div class="post-metadata">

**Author:** ![Diger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/diger/32/36786_2.png) [@Diger](https://discourse.julialang.org/u/Diger)\
**Post date:** [March 13, 2020, 10:06pm UTC](https://discourse.julialang.org/t/convert-float-to-integer/35948/5 "2020-03-13T22:06:21Z")

</div>

So you mean I should wrap it inside a function?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 13, 2020, 10:17pm UTC](https://discourse.julialang.org/t/convert-float-to-integer/35948/6 "2020-03-13T22:17:07Z")

</div>

Julia requires use of functions for good performance. Also, be careful when benchmarking that you are timing the code you mean to. If Julia can see a way to ignore running your code and replace it with faster code that gives the same answer, the compiler is free to do so.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [March 13, 2020, 10:31pm UTC](https://discourse.julialang.org/t/convert-float-to-integer/35948/7 "2020-03-13T22:31:20Z")

</div>

It’s also good to get used to some rough estimations when benchmarking. A single instruction e.g. `+` on two primitive types like `Float64` takes a few nanoseconds. If you have an array of 1\_000\_000 `Float64`s, it will roughly take 1\_000\_000 x “few nanoseconds” which is around a few microseconds. You are even doing more in your example, so 14ms seems fine.

My main point is: if you’re doing some calculations on 1\_000\_000 numbers and measure a few nanoseconds, then either the compiler figured out a shortcut, or you are benchmarking something completely different. Current CPUs cannot do 1\_000\_000 instructions in ~2ns. No matter what kind of instructions these are, limited by their clock speed of a few GHz.

p.s. a petahertz CPU could do that

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 13, 2020, 10:57pm UTC](https://discourse.julialang.org/t/convert-float-to-integer/35948/8 "2020-03-13T22:57:24Z")

</div>

Your timings seem pretty reasonable. 14ms is what you should expect for this loop. You can try a slight tweak that is more idiomatic and ever so slightly faster:

```julia
foo(x) = Int(floor((x-1/x)/sqrt(3))) # this is your code
bar(x) = floor(Int, (x-1/x)/sqrt(3)) # this is what you should do

```

Benchmarks vary a bit, but here’s one:

```julia
julia> @btime foo(x) setup=(x=rand(1:10^6));
  16.067 ns (0 allocations: 0 bytes)

julia> @btime bar(x) setup=(x=rand(1:10^6));
  12.922 ns (0 allocations: 0 bytes)

```

Do that a million times, and you’ve got 12-16ms.

Your benchmarking code, however, is not optimal. It’s in global scope, and, also, only the last iteration of the loop is actually kept, which gives the compiler leeway to ditch the first 999999 iterations. There’s no point in looping like that, the `@benchmark` macro already runs many iterations to get statistics, so don’t do that yourself, you’re just getting in the way of the benchmarking.

**Edit:** Here’s another alternative:

```julia
baz(x) = floor(Int, (x^2-1) / (sqrt(3)*x))

julia> @btime baz(x) setup=(x=rand(1:10^6));
  10.619 ns (0 allocations: 0 bytes)

```
