Salut cython specialists,
I would like to implement a routine adding dictionaries with possibly
different keys pointwise and delete all zero entries in the end:
sage: def dict_sum( list_of_dics, remove_zeros=True ):
... return_dict = {}
... for D in list_of_dics:
... for key in D:
... if key in return_dict:
... return_dict[ key ] += D[ key ]
... else:
... return_dict[ key ] = D[ key ]
...
... if remove_zeros:
... return_dict = dict( [ (key,value) for key,value in
d.iteritems() if value != 0 ] )
... return return_dict
sage: d = dict(zip(range(100000),range(0,100000,2)))
sage: timeit("p = dict_sum([d]*10, True)")
sage: timeit("p = dict_sum([d]*10, False)")
5 loops, best of 3: 216 ms per loop
5 loops, best of 3: 138 ms per loop
Improving it a little and putting it into cython gives
sage: %cython
sage: def dict_sum_cython( list_of_dicts, remove_zeros=True ):
... return_dict = list_of_dicts[0].copy()
... for D in list_of_dicts[1:]:
... for key in D:
... if key in return_dict:
... return_dict[key] += D[key]
... else:
... return_dict[key] = D[key]
...
... if remove_zeros:
... for_removal = [key for (key,value) in
return_dict.iteritems() if not value]
... for key in for_removal:
... del return_dict[key]
... return return_dict
sage: timeit("p = dict_sum_cython([d]*10, True)")
sage: timeit("p = dict_sum_cython([d]*10, False)")
5 loops, best of 3: 56 ms per loop
5 loops, best of 3: 57.8 ms per loop
This is already much faster! Any ideas how to improve the speed now?
All I know about the variables is that all dict values live in a
common ring ( mostly QQ ), can I use this somehow?
Thanks for your help, Christian
--
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org