对于数据量是明确的,数组可以是固定长度,使用 Vector 很方便,但是既然确定了长度,那么使用 Vector{T}(undef, 7) 这种方式来创建一维向量是否能提高内存的利用率?
# ~ 确定这个结构体只能存两个数
mutable struct IntDiff <: Compound
comps::Vector{Term}
function IntDiff(t1, t2)
args = Vector{Term}(undef, 2)
@inbounds args[1] = t1
@inbounds args[2] = t2
new(args)
end
end
这样子和直接 IntDiff([a,b]) 比起来会有内存利用方面的提升么?还是多此一举?
emm,我 benchmark 跑了一遍,确实是多此一举,甚至 new([a,b])这样还快 1ns ![]()