# Too many allocations?

**URL:** <https://discourse.julialang.org/t/too-many-allocations/55478>\
**Category:** Performance\
**Created:** [February 17, 2021, 5:31pm UTC](https://discourse.julialang.org/t/too-many-allocations/55478 "2021-02-17T17:31:19Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 17, 2021, 10:09pm UTC](https://discourse.julialang.org/t/too-many-allocations/55478/22 "2021-02-17T22:09:05Z")

</div>

Nice. I didn’t think about that interpretation. It was just the rounding to an abstract type seems strange. Which is the function that returns other type of number of the same “size”, given one type? Meaning

```julia
f(::Float32) = Int32
f(::Float64) = Int64
f(::Int32) = Float32
etc.

```

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [February 17, 2021, 10:29pm UTC](https://discourse.julialang.org/t/too-many-allocations/55478/23 "2021-02-17T22:29:27Z")

</div>

In this case this “mapping” seems implemented for `trunc, floor, ceil, round` as `@edit` tells me. By the way, `round(Integer, Float32(1.1))` gives `Int64`, not `Int32` (same for Float16). I don’t really know if this is intended, but as I just asked for an integer type, it seems reasonable that julia gives me the machine integer type if it can fit x.

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [February 17, 2021, 11:55pm UTC](https://discourse.julialang.org/t/too-many-allocations/55478/24 "2021-02-17T23:55:25Z")

</div>

It’s a bit tricky because of inexact mapping from floats to integers. For example, `Float32` can represent 2^31 perfectly, whereas `Int32` cannot:

```julia
julia> Integer(Float32(2^31))
2147483648

julia> round(Int32,Float32(2^31))
ERROR: InexactError: trunc(Int32, 2.1474836e9)

```

It should also be possible to create similar errors when rounding certain `Float64` to `Int64`. It’s unclear what the intention should be with such edge cases, and it’s probably unreasonable to play it safe by returning `BigInt` for `round(Integer, Float32(x))`. Some might say it’s not great to default to `round(Int, Float32(x))` because the result can then differ between 32- and 64-bit CPUs. I guess Python will use bigger integers when needed, but with a big trade-off in performance.

Only the coder knows their use case, and they are probably better off being concrete, `round(Int32, Float32(x))` when they know it will work.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 18, 2021, 9:06am UTC](https://discourse.julialang.org/t/too-many-allocations/55478/25 "2021-02-18T09:06:28Z")

</div>

A somewhat more basic suggestion:

> [@lancejnelson](#):
>
> ```julia
> omega = zeros(Float64,N+1,1)
> saveTheta = zeros(Float64,M,1)
> saveOmega = zeros(Float64,M,1)
> omega = zeros(Float64,N+1,1)
> 
> ```

here you are pre-allocating `omega` twice it seems?

[Previous page](https://discourse.julialang.org/t/too-many-allocations/55478.md?page=1)
