Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays “.=”什么时候比“=”更有效?_Arrays_Performance_Julia_Microbenchmark - Fatal编程技术网

Arrays “.=”什么时候比“=”更有效?

Arrays “.=”什么时候比“=”更有效?,arrays,performance,julia,microbenchmark,Arrays,Performance,Julia,Microbenchmark,使用BenchmarkTools考虑以下REPL行: julia> N = 10^2; M = collect(reshape(1:N^2,N,N)); e = collect(1:N); # N=100 julia> @btime M[:,1] .= e; @btime M[:,1] = e; 1.211 μs (6 allocations: 128 bytes) 364.623 ns (1 allocation: 16 bytes) julia> N = 10^3;

使用
BenchmarkTools
考虑以下REPL行:

julia> N = 10^2; M = collect(reshape(1:N^2,N,N)); e = collect(1:N); # N=100
julia> @btime M[:,1] .= e; @btime M[:,1] = e;
  1.211 μs (6 allocations: 128 bytes)
  364.623 ns (1 allocation: 16 bytes)
julia> N = 10^3; M = collect(reshape(1:N^2,N,N)); e = collect(1:N); # N=1000
julia> @btime M[:,1] .= e; @btime M[:,1] = e;
  1.511 μs (6 allocations: 128 bytes)
  1.634 μs (1 allocation: 16 bytes)
julia> N = 10^4; M = collect(reshape(1:N^2,N,N)); e = collect(1:N); # N=10000
julia> @btime M[:,1] .= e; @btime M[:,1] = e;
  3.514 μs (6 allocations: 128 bytes)
  13.230 μs (1 allocation: 16 bytes)

似乎
=
=
更有效,但仅适用于大N。我仍然不太了解引擎盖下发生的事情,也没有在Julia文档中找到解释。我应该在什么时候使用一个或另一个?

=
在这里总是首选,但您希望
@btime$M[:,1]。=$e
更准确地计时,否则您将看到全局范围的影响。@mcabbott,谢谢您的回答。问题在于基准测试,
=
总是更好。您不想发布它吗?
=
在这里总是首选,但您希望
@btime$M[:,1]。=$e
能够更准确地计时,否则您将看到全局范围的影响。@mcabbott,谢谢您的回答。问题在于基准测试,
=
总是更好。你不想把它寄出去吗?