Generally I want to make
f(g("a"), g("c"), g("k"))
with
f(map(g, ["a","c","k"])...)
be efficient and don’t generate allocations.
And I started experimenting, by creating 3 functions:
function foo_tuple(a,b,c)
+((a+1,b+2,c+3)...)
end
function foo_arr(a,b,c)
+([a+1,b+2,c+3]...)
end
function foo(a,b,c)
+(Tuple(i+j for (i,j) in zip(1:3,(a,b,c)))...)
end
Benchmarks are below:
julia> @btime foo(1,2,3)
247.813 ns (2 allocations: 112 bytes)
12
julia> @btime foo_arr(1,2,3)
93.243 ns (1 allocation: 80 bytes)
12
julia> @btime foo_tuple(1,2,3)
1.600 ns (0 allocations: 0 bytes)
12
And now I have several questions:
- Is there an efficient way to make map return tuple?
- How can I create tuples from arrays efficiently?
Tuple([1,2,3])
is 150 times slower thantuple(1,2,3)
I know, that splat isn’t preferable tool for big number of arguments, but with RGBA it will be cool to know how to pass parameters with this.