Skip to content

Instantly share code, notes, and snippets.

@Gnimuc
Created November 17, 2015 04:23
Show Gist options
  • Save Gnimuc/2d10aadd4bc765b96056 to your computer and use it in GitHub Desktop.
Save Gnimuc/2d10aadd4bc765b96056 to your computer and use it in GitHub Desktop.
[SO] Multiple selection from Julia array
function foo(genconv, I, J)
genconv[sub2ind(size(genconv),I,J)]
end
function bar(genconv, I, J)
diag(sub(genconv, I, J))
end
function baz(genconv, I, J)
[genconv[i,j] for (i,j) in zip(I, J)]
end
function benchmark()
genconv = rand(10000, 20000)
I = rand(collect(1:size(genconv)[1]), 10000)
J = rand(collect(1:size(genconv)[2]), 10000)
# warmup
foo(genconv, I, J)
bar(genconv, I, J)
baz(genconv, I, J)
println("foo:")
@time foo(genconv, I, J)
println("bar:")
@time bar(genconv, I, J)
println("baz:")
@time baz(genconv, I, J)
return "finished"
end
benchmark()
@Gnimuc
Copy link
Author

Gnimuc commented Nov 17, 2015

julia> benchmark()
foo:
  0.000447 seconds (7 allocations: 156.438 KB)
bar:
  0.000904 seconds (25 allocations: 157.078 KB)
baz:
  0.000368 seconds (3 allocations: 78.219 KB)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment