#14214: Cythoned homsets
----------------------------------------------+-----------------------------
Reporter: SimonKing | Owner: tbd
Type: enhancement | Status: needs_work
Priority: major | Milestone: sage-5.9
Component: performance | Resolution:
Keywords: Hom, cython, cached method | Work issues: Do not change
cmp too much
Report Upstream: N/A | Reviewers:
Authors: Simon King | Merged in:
Dependencies: #14159, #12951, #13184 | Stopgaps:
----------------------------------------------+-----------------------------
Comment (by nbruin):
Hm, two observations:
- the tests below do not corroborate that cached function lookup is
faster than simple attribute lookup on python classes
- It seems method resolution on extension classes is WAY slower than on
`object` derived classes.
First `Parent` derived.
{{{
sage: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:class T(sage.structure.parent.Parent):
: w=100
: def __init__(self):
: self.v=100
: @cached_method
: def t(self):
: return 100
:--
sage:
sage: P=T
sage: for i in [1..1000]:
....: P=type("T%d"%i,(P,),{"a%i"%i:1,"b%i"%i:2})
....: globals()["T%d"%i]=P
....:
sage: C=T1000(); c0=T()
sage: timeit('C.v',number=20000)
20000 loops, best of 3: 26 µs per loop
sage: timeit('c0.v',number=20000)
20000 loops, best of 3: 111 ns per loop
sage: timeit('C.w',number=20000)
20000 loops, best of 3: 22.6 µs per loop
sage: timeit('c0.w',number=20000)
20000 loops, best of 3: 73.8 ns per loop
sage: timeit('C.t()',number=20000)
20000 loops, best of 3: 38.3 µs per loop
sage: timeit('c0.t()',number=20000)
20000 loops, best of 3: 101 ns per loop
}}}
As you can see, everything on `C` is way slower than on `c0`, probably
because there's a 1000 deep MRO to contend with. However, I see no
indication that `cached_method` is ever faster than a straight attribute
lookup.
Next `object` derived:
{{{
sage: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:class T(object):
: w=100
: def __init__(self):
: self.v=100
: @cached_method
: def t(self):
: return 100
:--
sage:
sage: P=T
sage: for i in [1..1000]:
....: P=type("T%d"%i,(P,),{"a%i"%i:1,"b%i"%i:2})
....: globals()["T%d"%i]=P
....:
sage: C=T1000(); c0=T()
sage: timeit('C.v',number=200000)
200000 loops, best of 3: 69.8 ns per loop
sage: timeit('c0.v',number=200000)
200000 loops, best of 3: 66.7 ns per loop
sage: timeit('C.w',number=200000)
200000 loops, best of 3: 53.6 ns per loop
sage: timeit('c0.w',number=200000)
200000 loops, best of 3: 54.1 ns per loop
sage: timeit('C.t()',number=200000)
200000 loops, best of 3: 77.4 ns per loop
sage: timeit('c0.t()',number=200000)
200000 loops, best of 3: 77.6 ns per loop
}}}
No penalty at all for large inheritance depth! Same pattern: instance
attribute is a little slower than a class attribute and cached_method is
always a little slower than either.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/14214#comment:26>
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.