我有一个返回数组的函数。我想将函数映射到输入向量,输出是所有数组的简单串联。功能是:
function log_it(r, bzero = 0.25, N = 400)
main = rand(Float16, (N+150));
main[1] = bzero;
for i in 2:N+150
main[i] = *(r, main[i-1], (1-main[i-1]))
end;
y = unique(main[(N+1):(N+150)]);
r_vec = repeat([r], size(y)[1]);
hcat(r_vec, y)
end;
Run Code Online (Sandbox Code Playgroud)
我可以很好地映射它:
map(log_it, 2.4:0.001:2.405)
Run Code Online (Sandbox Code Playgroud)
但结果很糟糕:
[2.4 0.58349609375]
[2.401 0.58349609375]
[2.402 0.583984375; 2.402 0.58349609375]
[2.403 0.583984375]
[2.404 0.583984375]
[2.405 0.58447265625; 2.405 0.583984375]
Run Code Online (Sandbox Code Playgroud)
注意,嵌套数组的长度是无限的——我正在寻找一种不依赖于提前知道嵌套数组长度的解决方案。
我想要的是这样的:
2.4 0.583496
2.401 0.583496
2.402 0.583984
2.402 0.583496
2.403 0.583984
2.404 0.583984
2.405 0.584473
2.405 0.583984
Run Code Online (Sandbox Code Playgroud)
我使用 for 循环制作的:
results = Array{Float64, 2}(undef, 0, 2)
for i in 2.4:0.001:2.405
results = cat(results, log_it(i), dims = 1)
end
results
Run Code Online (Sandbox Code Playgroud)
代码工作正常,但 for 循环需要大约四倍的时间。我也觉得 map 是正确的方法,我只是遗漏了一些东西 - 要么以返回一个不错的数组向量的方式执行 map,要么在数组的某些突变中“unnest”。我试过查看 flatten 和 collect 之类的功能,但找不到任何东西。
提前谢谢了!
您确定您正确地对此进行了基准测试吗?尤其是在操作非常快的情况下,基准测试有时会很棘手。作为起点,我建议您确保始终将要进行基准测试的任何代码包装到一个函数中,并使用BenchmarkTools包来获得可靠的计时。
在 Julia 中编写循环通常不应该有性能损失,因此与map听起来可疑的循环相比,循环的运行时间增加了 3 倍。
这是我得到的:
julia> using BenchmarkTools
julia> @btime map(log_it, 2.4:0.001:2.405)
121.426 ?s (73 allocations: 14.50 KiB)
julia> function with_loop()
results = Array{Float64, 2}(undef, 0, 2)
for i in 2.4:0.001:2.405
results = cat(results, log_it(i), dims = 1)
end
results
end
julia> @btime with_loop()
173.492 ?s (295 allocations: 23.67 KiB)
Run Code Online (Sandbox Code Playgroud)
所以循环慢了大约 50%,但那是因为你分配了更多。
当您使用时map,通常有一种更 Julia 的方式来表达您使用broadcast所做的事情。这适用于任何用户定义的函数:
julia> @btime log_it.(2.4:0.001:2.405)
121.434 ?s (73 allocations: 14.50 KiB)
Run Code Online (Sandbox Code Playgroud)
相当于你的map表情。我认为您正在寻找的只是一种堆叠所有结果向量的方法 - 您可以使用vcat和拼凑:
julia> @btime vcat(log_it.(2.4:0.001:2.405)...)
122.837 ?s (77 allocations: 14.84 KiB)
Run Code Online (Sandbox Code Playgroud)
只是为了确认:
julia> vcat(log_it.(2.4:0.001:2.405)...) == with_loop()
true
Run Code Online (Sandbox Code Playgroud)
因此,在map解决方案的速度和内存成本下,使用广播和连接可提供与循环相同的结果。