Skip to content

Instantly share code, notes, and snippets.

@Gnimuc
Created August 1, 2018 13:24
Show Gist options
  • Save Gnimuc/376705b89b61150a79b527d57ad71214 to your computer and use it in GitHub Desktop.
Save Gnimuc/376705b89b61150a79b527d57ad71214 to your computer and use it in GitHub Desktop.
A = sprand(65535, 65500, 1e-5)
v = rand(65500)
function foo(A, v)
b = zeros(size(A,1))
rows = rowvals(A)
vals = nonzeros(A)
m, n = size(A)
for i = 1:n
@inbounds @simd for j in nzrange(A, i)
row = rows[j]
val = vals[j]
b[row] += val * v[i]
end
end
b
end
# COO?
idxcoo = Tuple{Int,Int}[]
valcoo = Float64[]
rows = rowvals(A)
vals = nonzeros(A)
m, n = size(A)
for i = 1:n
for j in nzrange(A, i)
row = rows[j]
val = vals[j]
push!(idxcoo, (row,i))
push!(valcoo, val)
end
end
function bar(idxcoo, valcoo, v, N)
b = zeros(N)
@inbounds @simd for n in eachindex(idxcoo)
i, j = idxcoo[n]
val = valcoo[n]
b[i] += val * v[j]
end
b
end
using BenchmarkTools
@benchmark foo($A, $v) # -> 687
@benchmark bar($idxcoo, $valcoo, $v, size(A,1)) # -> 193
foo2() = [foo(A,v) for i = 1:1000]
bar2() = [bar(idxcoo, valcoo, v, size(A,1)) for i = 1:1000]
foo(A, v) == bar(idxcoo, valcoo, v, size(A,1))
using Profile
Profile.clear()
@profiler foo2()
Profile.clear()
@profiler bar2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment