#12601: @cached_method does not work for special methods
-------------------------------------------------+-------------------------
Reporter: saraedum | Owner: jason
Type: enhancement | Status:
Priority: minor | needs_review
Component: misc | Milestone: sage-5.13
Keywords: cached_method, cache, | Resolution:
operator, special method | Merged in:
Authors: Simon King | Reviewers:
Report Upstream: Completely fixed; Fix | Work issues:
reported upstream | Commit:
Branch: | Stopgaps:
Dependencies: #15038 |
-------------------------------------------------+-------------------------
Comment (by SimonKing):
PS: If the list only contains three elements (as in your not very
realistic example), then still the string methods are faster.
{{{
sage: cython("""
cdef list special_methods = ['__get__', '__set__', '__hash__']def
is_special1(str method): return method in special_methodsdef
is_special2(str method): return method.startswith('__') and
method.endswith('__')"""
) ....:
sage: timeit("is_special1('__hash__')",number=100000)
100000 loops, best of 3: 174 ns per loop
sage: timeit("is_special2('__hash__')",number=100000)
100000 loops, best of 3: 159 ns per loop
}}}
PPS: When we use a frozenset instead, then using the string methods is not
faster any more, even with the complete set of special attribute names.
{{{
sage: cython("""
cdef frozenset special_methods = frozenset(['__abs__', '__add__',
'__and__', '__call__', '__cmp__', '__coerce__', '__contains__', '__del__',
'__delattr__', '__delete__', '__delitem__', '__delslice__', '__div__',
'__eq__', '__float__', '__floordiv__', '__ge__', '__get__', '__getattr__',
'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__',
'__hex__', '__iadd__', '__iand__', '__idiv__', '__ifloordiv__',
'__ilshift__', '__imod__', '__imul__', '__index__', '__init__', '__int__',
'__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__',
'__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', '__long__',
'__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__',
'__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__',
'__radd__', '__rand__', '__rdiv__', '__repr__', '__rfloordiv__',
'__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__',
'__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__',
'__set__', '__setattr__', '__setitem__', '__setslice__', '__str__',
'__sub__', '__truediv__', '__xor__', 'next'])
def is_special1(str method):
return method in special_methods
def is_special2(str method):
return method.startswith('__') and method.endswith('__')
"""
)
....:
sage: timeit("is_special1('__hash__')",number=100000)
100000 loops, best of 3: 113 ns per loop
sage: timeit("is_special2('__hash__')",number=100000)
100000 loops, best of 3: 169 ns per loop
}}}
--
Ticket URL: <http://trac.sagemath.org/ticket/12601#comment:19>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.