# Allocation seem high for such a simple function?

**URL:** <https://discourse.julialang.org/t/allocation-seem-high-for-such-a-simple-function/47565>\
**Category:** New to Julia\
**Created:** [October 1, 2020, 2:52am UTC](https://discourse.julialang.org/t/allocation-seem-high-for-such-a-simple-function/47565 "2020-10-01T02:52:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 1, 2020, 2:52am UTC](https://discourse.julialang.org/t/allocation-seem-high-for-such-a-simple-function/47565/1 "2020-10-01T02:52:16Z")

</div>

```julia
minimumby(f, itr) = itr[argmin(map(f, itr))]
@time minimumby(x->x^2, [-2, 1, 2, 3, 4])

```

I get

```julia
 0.044858 seconds (48.96 k allocations: 2.505 MiB)

```

This is quite slow and a lot of allocations

But if I give the function a name then it’s fast

```julia
f(x) = x^2

@time minimumby(f, [-2, 1, 2, 3, 4])

```

```julia
0.000009 seconds (3 allocations: 272 bytes)

```

Wondering why can’t Julia just “compile” the anonymous function away ?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 1, 2020, 3:24am UTC](https://discourse.julialang.org/t/allocation-seem-high-for-such-a-simple-function/47565/2 "2020-10-01T03:24:44Z")

</div>

Because you are just observing the anonymous function being compiled away. Your `x->x^2` create a new function everytime you typed that in.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 1, 2020, 3:38am UTC](https://discourse.julialang.org/t/allocation-seem-high-for-such-a-simple-function/47565/3 "2020-10-01T03:38:18Z")

</div>

I see. I thought it would hash the code and see thst it’s not new or something
