Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Allocation seem high for such a simple function?

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

I get

 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

f(x) = x^2

@time minimumby(f, [-2, 1, 2, 3, 4])
0.000009 seconds (3 allocations: 272 bytes)

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

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles