Last active
October 12, 2017 23:26
-
-
Save simonbyrne/43a4041c76e18f79837b06dec459b820 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 2_500_000 | |
p = 1.9e-5 | |
X = sprand(n,n,p) | |
v = sprand(n,8/n) | |
function fast_A_mul_B(X::SparseMatrixCSC{TX,IX},v::SparseVector{TV,IV}) where {TX,IX,TV,IV} | |
X.n == v.n || throw(DimensionMismatch()) | |
ind_out = promote_type(IX,IV)[] | |
val_out = promote_type(TX,TV)[] | |
for (vi,vv) in zip(v.nzind, v.nzval) | |
lo_ptr = X.colptr[vi] | |
hi_ptr = X.colptr[vi+1] - 1 | |
append!(ind_out, X.rowval[lo_ptr:hi_ptr]) | |
append!(val_out, X.nzval[lo_ptr:hi_ptr] * vv) | |
end | |
sparsevec(ind_out, val_out, X.m) | |
end | |
@time X*v # 0.018 seconds | |
@time fast_A_mul_B(X,v) # 0.000_051 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment