# Why is Vector{Float64} much faster than Vector{Float32}?

**URL:** <https://discourse.julialang.org/t/why-is-vector-float64-much-faster-than-vector-float32/10328>\
**Category:** Performance\
**Created:** [April 14, 2018, 3:16am UTC](https://discourse.julialang.org/t/why-is-vector-float64-much-faster-than-vector-float32/10328 "2018-04-14T03:16:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [April 14, 2018, 3:16am UTC](https://discourse.julialang.org/t/why-is-vector-float64-much-faster-than-vector-float32/10328/1 "2018-04-14T03:16:08Z")

</div>

I have two programs that are almost identical except that in one program I used Float32 vectors or arrays while in the other one I used Float64. The Float64 program (named prog\_f64.jl) is about 5x faster than the Float32 one (named proj\_f32.jl). Does anybody know why such problem happens? You can see my programs here:  
[https://gist.github.com/jinliangwei/74d3871bebc1251b651cc6fd511fed08](https://gist.github.com/jinliangwei/74d3871bebc1251b651cc6fd511fed08)

Here is an input data file if you’d like to run the programs yourself: [http://www.cs.cmu.edu/~jinlianw/data/ratings.csv](http://www.cs.cmu.edu/~jinlianw/data/ratings.csv)

BTW, my Julia version is 0.6.2.

---

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [April 14, 2018, 5:07am UTC](https://discourse.julialang.org/t/why-is-vector-float64-much-faster-than-vector-float32/10328/2 "2018-04-14T05:07:59Z")

</div>

Sorry about the spam. Now I see why the problem happens. It’s basically caused by line 86 and line 87:  
[https://gist.github.com/jinliangwei/74d3871bebc1251b651cc6fd511fed08#file-proj\_f32-jl-L86](https://gist.github.com/jinliangwei/74d3871bebc1251b651cc6fd511fed08#file-proj_f32-jl-L86)

`alpha` and `0.5` are of type Float64, so there were lots of type conversion.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [April 14, 2018, 2:21pm UTC](https://discourse.julialang.org/t/why-is-vector-float64-much-faster-than-vector-float32/10328/3 "2018-04-14T14:21:03Z")

</div>

Also, `sqrt` should be faster than `^0.5`, and is generic with respect to `Float` types.

That being said, constants are always a pain for me when writing generic code that is supposed to work for `Int16` / `Int32` / `Int64` or `Float32` / `Float64`. I tend to get the needed type somewhere, e.g. using `typeof`, `eltype` or `where` or an explicit `float_t` parameter, and then have `convert` all over the place. This looks somewhat ugly and I am not sure how well this plays with autodiff, but I know no better way. Anyone else has better tips?

---

<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:** [April 14, 2018, 4:07pm UTC](https://discourse.julialang.org/t/why-is-vector-float64-much-faster-than-vector-float32/10328/4 "2018-04-14T16:07:06Z")

</div>

> [@foobar\_lv2](#):
>
> That being said, constants are always a pain for me when writing generic code that is supposed to work for Int16 / Int32 / Int64 or Float32 / Float64. I tend to get the needed type somewhere, e.g. using typeof, eltype or where or an explicit float\_t parameter, and then have convert all over the place. This looks somewhat ugly and I am not sure how well this plays with autodiff, but I know no better way.

It would be possible to have a `@literal_type T begin ... end` macro that would rewrite an expression, converting all numeric literals to type, `T`. That would allow you to write your code with natural literal syntax and have them converted to the type of some type parameter.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 14, 2018, 4:55pm UTC](https://discourse.julialang.org/t/why-is-vector-float64-much-faster-than-vector-float32/10328/5 "2018-04-14T16:55:43Z")

</div>

> **[GitHub - JuliaMath/ChangePrecision.jl: macro to change the default...](https://github.com/JuliaMath/ChangePrecision.jl)**
>
> macro to change the default floating-point precision in Julia code - GitHub - JuliaMath/ChangePrecision.jl: macro to change the default floating-point precision in Julia code

But not sure it can currently be used be runtime types. At least extending it to putting in the converts should be possible.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 14, 2018, 5:52pm UTC](https://discourse.julialang.org/t/why-is-vector-float64-much-faster-than-vector-float32/10328/6 "2018-04-14T17:52:19Z")

</div>

> [@kristoffer.carlsson](#):
>
> At least extending it to putting in the converts should be possible.

Converting only literals from `x` to `T(x)` is much easier than what ChangePrecision.jl currently does, so it should probably be a separate macro (but could be in the same package; PRs welcome).
