-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
three-argument [i]permute! methods #22815
Comments
In the second case, wouldn't |
Yes, that does the trick! Good catch! :) |
And in the first case, wouldn't |
Clever! :) This issue then reduces to two questions: (1) Are three-argument Regarding (2): For not-small containers, For small containers (e.g. n ~ 10, see below), julia> function permute!(dest, src, perm)
for (i, pi) in enumerate(perm)
dest[i] = src[pi]
end
return dest
end
permute! (generic function with 1 method)
julia> function ipermute!(dest, src, perm)
for (i, pi) in enumerate(perm)
dest[pi] = src[i]
end
return dest
end
ipermute! (generic function with 1 method)
julia> using BenchmarkTools
julia> begin
n = 10
foo = rand(n)
bar = similar(foo)
perm = randperm(n)
end;
julia> @benchmark ipermute!($bar, $foo, $perm)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 12.966 ns (0.00% GC)
median time: 13.803 ns (0.00% GC)
mean time: 15.357 ns (0.00% GC)
maximum time: 236.890 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 998
julia> @benchmark $bar[$perm] = $foo
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 18.628 ns (0.00% GC)
median time: 18.683 ns (0.00% GC)
mean time: 21.302 ns (0.00% GC)
maximum time: 291.829 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 997
julia> @benchmark permute!($bar, $foo, $perm)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 13.031 ns (0.00% GC)
median time: 14.247 ns (0.00% GC)
mean time: 15.802 ns (0.00% GC)
maximum time: 285.616 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 998
julia> @benchmark $bar .= @view $foo[$perm]
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 21.225 ns (0.00% GC)
median time: 22.537 ns (0.00% GC)
mean time: 24.698 ns (0.00% GC)
maximum time: 140.972 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 997
begin
n = 100
foo = rand(n)
bar = similar(foo)
perm = randperm(n)
end;
julia> @benchmark permute!($bar, $foo, $perm)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 129.426 ns (0.00% GC)
median time: 129.462 ns (0.00% GC)
mean time: 146.328 ns (0.00% GC)
maximum time: 541.591 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 886
julia> @benchmark $bar .= @view $foo[$perm]
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 185.797 ns (0.00% GC)
median time: 190.965 ns (0.00% GC)
mean time: 213.428 ns (0.00% GC)
maximum time: 1.447 μs (0.00% GC)
--------------
samples: 10000
evals/sample: 671 Best! |
Being able to permute one vector with the result stored in another --- without allocation --- is now and again quite handy. For example, this functionality could improve #22774, and I had use for such methods last week in some research code. Naive implementations are as simple as
Perhaps such methods have a place alongside the two-argument
[i]permute!
methods existing in base? Best!The text was updated successfully, but these errors were encountered: