For me using A[i,:] = -A[i,:]
is quicker than flipsign
if I just test the sign of A[i,i]
once per row.
function adjust_signs(A)
for i in axes(A,1)
if sign(A[i,i]) < 0
A[i, :] = -A[i, :]
end
end
return A
end
The difference is a factor of 2 for 3x3 matrices, but is notably more as n gets larger.
julia> using LinearAlgebra
julia> using BenchmarkTools
julia>
julia> function correct_sign(A)
A .= sign.(view(A, diagind(A))) .* A
end
correct_sign (generic function with 1 method)
julia>
julia> function correct_sign_loop(A)
for i in axes(A, 1)
s = sign(A[i, i])
for j in axes(A, 2)
@inbounds A[i, j] *= s
end
end
return A
end
correct_sign_loop (generic function with 1 method)
julia>
julia> function flip_signs(A)
@views for i in axes(A, 1)
A[i, :] .= flipsign.(A[i, :], A[i, i])
end
return A
end
flip_signs (generic function with 1 method)
julia>
julia> function adjust_signs(A)
for i in axes(A,1)
if sign(A[i,i]) < 0
A[i, :] = -A[i, :]
end
end
return A
end
adjust_signs (generic function with 1 method)
julia>
julia> n = 3
3
julia> B = randn(n,n);
julia> @benchmark correct_sign($B)
BenchmarkTools.Trial: 10000 samples with 968 evaluations.
Range (min β¦ max): 79.071 ns β¦ 1.360 ΞΌs β GC (min β¦ max): 0.00% β¦ 90.95%
Time (median): 86.260 ns β GC (median): 0.00%
Time (mean Β± Ο): 89.630 ns Β± 43.153 ns β GC (mean Β± Ο): 3.20% Β± 5.94%
βββββββββ
ββ
ββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββ β
79.1 ns Histogram: frequency by time 104 ns <
Memory estimate: 112 bytes, allocs estimate: 3.
julia> B = randn(n,n);
julia> @benchmark correct_sign_loop($B)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 9.500 ns β¦ 21.916 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 10.250 ns β GC (median): 0.00%
Time (mean Β± Ο): 10.148 ns Β± 0.469 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β β
β
β β β β β β β β β
ββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββ β
9.5 ns Histogram: log(frequency) by time 10.5 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
julia> B = randn(n,n);
julia> @benchmark flip_signs($B)
BenchmarkTools.Trial: 10000 samples with 999 evaluations.
Range (min β¦ max): 9.259 ns β¦ 20.562 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 9.301 ns β GC (median): 0.00%
Time (mean Β± Ο): 9.349 ns Β± 0.376 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
9.26 ns Histogram: frequency by time 9.51 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
julia> B = randn(n,n);
julia> @benchmark adjust_signs($B)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min β¦ max): 5.791 ns β¦ 14.291 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 6.292 ns β GC (median): 0.00%
Time (mean Β± Ο): 6.311 ns Β± 0.169 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β β
β β β β
β
βββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββ β
5.79 ns Histogram: log(frequency) by time 6.46 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
julia>
julia> n = 30
30
julia> B = randn(n,n);
julia> @benchmark correct_sign($B)
BenchmarkTools.Trial: 10000 samples with 343 evaluations.
Range (min β¦ max): 255.224 ns β¦ 2.571 ΞΌs β GC (min β¦ max): 0.00% β¦ 88.50%
Time (median): 266.280 ns β GC (median): 0.00%
Time (mean Β± Ο): 271.691 ns Β± 55.239 ns β GC (mean Β± Ο): 1.57% Β± 5.73%
βββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
255 ns Histogram: log(frequency) by time 574 ns <
Memory estimate: 336 bytes, allocs estimate: 3.
julia> B = randn(n,n);
julia> @benchmark correct_sign_loop($B)
BenchmarkTools.Trial: 10000 samples with 320 evaluations.
Range (min β¦ max): 264.972 ns β¦ 354.947 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 292.319 ns β GC (median): 0.00%
Time (mean Β± Ο): 287.701 ns Β± 9.718 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β
β
β ββ ββ β
ββββββββββββββββββββββββ
βββββββββββ
ββββ
βββ
β
βββ
βββββ
ββββ
ββββ
β
β
β
265 ns Histogram: log(frequency) by time 311 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
julia> B = randn(n,n);
julia> @benchmark flip_signs($B)
BenchmarkTools.Trial: 10000 samples with 320 evaluations.
Range (min β¦ max): 272.784 ns β¦ 376.694 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 292.837 ns β GC (median): 0.00%
Time (mean Β± Ο): 293.674 ns Β± 4.581 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
β ββ ββ β
βββββββββββββββββββββββββββββββββ
β
βββ
ββββ
β
β
β
ββ
βββββ
β
βββ
ββββββ β
273 ns Histogram: log(frequency) by time 318 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
julia> B = randn(n,n);
julia> @benchmark adjust_signs($B)
BenchmarkTools.Trial: 10000 samples with 998 evaluations.
Range (min β¦ max): 20.206 ns β¦ 38.327 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 21.710 ns β GC (median): 0.00%
Time (mean Β± Ο): 21.773 ns Β± 0.549 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
βββ
βββ βββ β
ββββββββββββββββββββββββββββββββββββ
ββββββ
β
ββββββββββββββββ β
20.2 ns Histogram: log(frequency) by time 23.2 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
julia>
julia> n = 1000
1000
julia> B = randn(n,n);
julia> @benchmark correct_sign($B)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min β¦ max): 164.917 ΞΌs β¦ 2.012 ms β GC (min β¦ max): 0.00% β¦ 90.28%
Time (median): 178.542 ΞΌs β GC (median): 0.00%
Time (mean Β± Ο): 179.446 ΞΌs Β± 22.207 ΞΌs β GC (mean Β± Ο): 0.17% Β± 1.25%
β
βββ
βββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββ β
165 ΞΌs Histogram: frequency by time 188 ΞΌs <
Memory estimate: 7.97 KiB, allocs estimate: 4.
julia> B = randn(n,n);
julia> @benchmark correct_sign_loop($B)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min β¦ max): 364.917 ΞΌs β¦ 463.750 ΞΌs β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 381.333 ΞΌs β GC (median): 0.00%
Time (mean Β± Ο): 381.398 ΞΌs Β± 3.586 ΞΌs β GC (mean Β± Ο): 0.00% Β± 0.00%
ββ
βββββ
βββββββββββββββββββββββ
βββ
βββββ
βββββββββββββ
β
ββββββββββββββββ β
365 ΞΌs Histogram: frequency by time 393 ΞΌs <
Memory estimate: 0 bytes, allocs estimate: 0.
julia> B = randn(n,n);
julia> @benchmark flip_signs($B)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min β¦ max): 381.708 ΞΌs β¦ 498.958 ΞΌs β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 390.750 ΞΌs β GC (median): 0.00%
Time (mean Β± Ο): 391.702 ΞΌs Β± 4.005 ΞΌs β GC (mean Β± Ο): 0.00% Β± 0.00%
βββ
βββββββββ
β
β
β
β
β
β
βββββββββββββ βββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
382 ΞΌs Histogram: log(frequency) by time 403 ΞΌs <
Memory estimate: 0 bytes, allocs estimate: 0.
julia> B = randn(n,n);
julia> @benchmark adjust_signs($B)
BenchmarkTools.Trial: 10000 samples with 178 evaluations.
Range (min β¦ max): 600.657 ns β¦ 792.371 ns β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 641.382 ns β GC (median): 0.00%
Time (mean Β± Ο): 643.524 ns Β± 8.269 ns β GC (mean Β± Ο): 0.00% Β± 0.00%
βββ β ββ β
ββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββ
β
β
β
β
β
601 ns Histogram: log(frequency) by time 684 ns <
Memory estimate: 0 bytes, allocs estimate: 0.