Well, this brings me to explain my whole problem which is broader than the initial topic of this thread:
I have a black-box library that generates vectors of integers. For simplicity, let’s say my input is the following vector inp
, where I replaced the actual values of inp
by ordered numbers so that we can see where each one goes in the desired output:
julia> w = h = 3
3
julia> inp = collect(1:3*w*h)
27-element Array{Int64,1}:
1
2
3
⋮
25
26
27
Basically, this input is a flat version of a 3D array and here I am assuming that its size is (3, w, h)
. I cannot modify the way my input is provided.
Then, my output should be the following vector of arrays out
:
julia> out = f1(inp)
3-element Array{Array{Int64,2},1}:
[1 10 19; 4 13 22; 7 16 25]
[2 11 20; 5 14 23; 8 17 26]
[3 12 21; 6 15 24; 9 18 27]
It has been generated with the function f1
below:
julia> function f1(x::Array{Int64,1})
w = h = 3
reshaped = reshape(x, (3,w,h))
[Array{Int64}(reshaped[i,:,:]) for i in 1:3]
end
f1 (generic function with 1 method)
Given this, I would like to build a function that does:
- the same computation as
f1
,
- faster,
- and, whose output is of type
Array{Array{Int64,2},1}
.
So far, and because of the conversion to type Array{Array{Int64,2},1}
, I could only gain some speed-up with the following function f2
:
julia> function f2(x::Array{Int64,1})
w = h = 3
[convert(Array{UInt8,2}, reshape(@view(x[i:3:length(x)]), (w,h))) for i in 1:3]
end
f2 (generic function with 1 method)
julia> @btime f1(inp)
389.115 ns (8 allocations: 1.13 KiB)
julia> @btime f2(inp)
254.549 ns (4 allocations: 592 bytes)