Last active
July 28, 2017 09:03
-
-
Save akstrfn/c3c62e5c798c2fa3211934bec3decc5a to your computer and use it in GitHub Desktop.
trying to figure out speed of various operation inside dok_matrix
This file contains 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
from time import time | |
from itertools import product | |
import numpy as np | |
from scipy.sparse import dok_matrix | |
from scipy.sparse.sputils import upcast_scalar | |
from scipy._lib.six import iterkeys | |
m = dok_matrix(np.random.random((100, 500))) | |
def ij_loop(): | |
res_dtype = upcast_scalar(m.dtype, 1) | |
new = dok_matrix(m.shape, dtype=res_dtype) | |
M, N = m.shape | |
for i in range(M): | |
for j in range(N): | |
aij = m.get((i, j), 0) + 1 | |
if aij: | |
new[i, j] = aij | |
return new | |
def ij_loop_no_get(): | |
res_dtype = upcast_scalar(m.dtype, 1) | |
new = dok_matrix(m.shape, dtype=res_dtype) | |
M, N = m.shape | |
for i in range(M): | |
for j in range(N): | |
aij = dict.get(m, (i, j), 0) + 1 | |
if aij: | |
new[i, j] = aij | |
return new | |
def product_loop(): | |
res_dtype = upcast_scalar(m.dtype, 1) | |
new = dok_matrix(m.shape, dtype=res_dtype) | |
M, N = m.shape | |
for i, j in product(range(M), range(N)): | |
aij = m.get((i, j), 0) + 1 | |
if aij: | |
new[i, j] = aij | |
return new | |
def product_loop_no_get(): | |
res_dtype = upcast_scalar(m.dtype, 1) | |
new = dok_matrix(m.shape, dtype=res_dtype) | |
M, N = m.shape | |
for i, j in product(range(M), range(N)): | |
aij = dict.get(m, (i, j), 0) + 1 | |
if aij: | |
new[i, j] = aij | |
return new | |
def keys_loop(): | |
res_dtype = upcast_scalar(m.dtype, 1) | |
new = dok_matrix(m.shape, dtype=res_dtype) | |
M, N = m.shape | |
for key in m.keys(): | |
new[key] = m[key] + 1 | |
return new | |
def iterkeys_loop(): | |
res_dtype = upcast_scalar(m.dtype, 1) | |
new = dok_matrix(m.shape, dtype=res_dtype) | |
M, N = m.shape | |
for key in iterkeys(m): | |
new[key] = m[key] + 1 | |
return new | |
if __name__ == "__main__": | |
import sys | |
import inspect | |
funcs = inspect.getmembers(sys.modules[__name__], inspect.isfunction) | |
funcs = [t for t in funcs if inspect.getmodule(t[1]) == sys.modules[__name__]] | |
res = funcs[0][1]() | |
runs = 100 | |
for fname, f in funcs: | |
t = time() | |
for _ in range(runs): | |
assert res.items() == f().items() | |
print(fname, (time() - t) / runs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment