I have a function that returns a tuple of two ints, and I want to find the largest int that shows up in each index. we’ve got this function in base called max which will do this for you, but the compilation time is kinda crazy:
julia> @time max.((0,0), [(1,1) for i=1:100]...)
47.772725 seconds (14.32 M allocations: 549.282 MiB, 2.91% gc time, 99.41% compilation time)
(1, 1)
julia> # this is logically equivalent, but >500x faster
julia> @time (max(0, [1 for i=1:100]...), max(0, [1 for i=1:100]...))
0.078314 seconds (68.24 k allocations: 3.324 MiB, 99.28% compilation time)
(1, 1)
it also recompiles for each new set of arguments, so max.((0,0), [(1,1) for i=1:101]...) will take a long time again, and then so will max.((0,0), [(1,1) for i=1:102]...) after those two.
is there some way I can get max to not compile that long, other than the obvious
julia> arr1 = arr2 = zeros(101)
julia> for (ind, i) in enumerate([(0, 0); [(1,1) for i=1:100]])
arr1[ind], arr2[ind] = i
end
julia> max(arr1...), max(arr2...)
I don’t want to run the tuple function (what I’ve been representing as [(0, 0); [(1,1) for i=1:100]]) more than I need to
note maximum([(0,0); [(1,1) for i=1:100]]) has different semantics. observe the difference here:
julia> max.((0,0), [([1:100;][i],[100:-1:1;][i]) for i=1:100]...)
(100, 100)
julia> maximum([(0,0); [([1:100;][i],[100:-1:1;][i]) for i=1:100]])
(100, 1)