I was surprised by this performance measurement (v.1.11.1):
julia> foo() = Int8[1 2 3; 4 5 6; 7 8 9]
foo (generic function with 1 method)
julia> bar() = Matrix{Int8}([1 2 3; 4 5 6; 7 8 9])
bar (generic function with 1 method)
julia> @btime foo()
203.167 ns (3 allocations: 176 bytes)
3×3 Matrix{Int8}:
1 2 3
4 5 6
7 8 9
julia> @btime bar()
68.238 ns (4 allocations: 240 bytes)
3×3 Matrix{Int8}:
1 2 3
4 5 6
7 8 9
bar
creates a Matrix{Int64}
which is later converted to Matrix{Int8}
, while foo
directly constructs the Matrix{Int8}
. So why is the performance opposite to what i expect?
It scales badly too. For 9x9 matrices the difference is almost a factor of 10.
For Vector
I see the expected behaviour:
julia> foo() = Int8[1, 2, 3, 4, 5, 6, 7, 8, 9]
foo (generic function with 1 method)
julia> bar() = Vector{Int8}([1, 2, 3, 4, 5, 6, 7, 8, 9])
bar (generic function with 1 method)
julia> @btime foo();
20.020 ns (2 allocations: 80 bytes)
julia> @btime bar();
44.646 ns (3 allocations: 176 bytes)