On Thursday, May 7, 2015 at 8:08:42 PM UTC+2, Douglas Bates wrote:
>
>
>
> On Thursday, May 7, 2015 at 11:55:59 AM UTC-5, Kristoffer Carlsson wrote:
>>
>> Boring but non allocating like list comprehension and anonymous function:
>>
>> m = typemin(Int16)
>> for TUnit in TUnitS
>> m = max(m,TUnit.tau_max)
>> end
>>
>>
> Even more compact is to use a function as the first argument to maximum
>
> julia> immutable foo
> bar::Int16
> baz::Float32
> end
>
> julia> [foo(i,0.) for i in 1:10]
> 10-element Array{foo,1}:
> foo(1,0.0f0)
> foo(2,0.0f0)
> foo(3,0.0f0)
> foo(4,0.0f0)
> foo(5,0.0f0)
> foo(6,0.0f0)
> foo(7,0.0f0)
> foo(8,0.0f0)
> foo(9,0.0f0)
> foo(10,0.0f0)
>
> julia> maximum(x->x.bar,ans)
> 10
>
>
>
If this is in a tight spot the anonymous function will cause a significant
slowdown:
function get_max(foos::Vector{foo})
m = typemin(Int16)
for foo in foos
m = max(m,foo.bar)
end
return m
end
foos = [foo(i,0.) for i in 1:10^4]
@time for i = 1:10^3
maximum(x->x.bar,foos)
end
# 0.53 seconds
get_max(foos)
@time for i = 1:10^3
get_max(foos)
end
# 0.013 seconds