On 2015-04-20, Simon King <simon.k...@uni-jena.de> wrote:
> Thank you! Is there a penalty for doing "type(self)" twice? I am
> thinking of PY_NEW(type(self)) versus type(self).__new__(type(self)),
> with typical application in the creation of elements in methods like
> _add_ or _mul_.

There is a penalty, but one can cope with it:
from sage.ext.memory cimport *
def test1(C):
    cdef size_t n
    for n from 0<=n<1000000:
        x = PY_NEW(type(C))
def test2(C):
    cdef size_t n
    for n from 0<=n<1000000:
        x = (type(C)).__new__(type(C))
def test1b(C):
    cdef size_t n
    cdef type T
    for n from 0<=n<1000000:
        T = type(C)
        x = PY_NEW(T)
def test2b(C):
    cdef size_t n
    cdef type T
    for n from 0<=n<1000000:
        T = type(C)
        x = T.__new__(T)

and we obtain (with a cythoned version of path algebra elements):

  sage: DiGraph({0:{1:['a'], 2:['b']}, 1:{0:['c'], 1:['d']}, 
2:{0:['e'],2:['f']}}).path_semigroup().algebra(ZZ).inject_variables()
  Defining e_0, e_1, e_2, a, b, c, d, e, f
  sage: %timeit test1(a)
  10 loops, best of 3: 101 ms per loop
  sage: %timeit test1b(a)
  10 loops, best of 3: 105 ms per loop
  sage: %timeit test2(a)
  10 loops, best of 3: 196 ms per loop
  sage: %timeit test2b(a)
  10 loops, best of 3: 102 ms per loop

Hence, doing (type(C)).__new__(type(C)) is clearly slower than
PY_NEW(type(C)), but when one assigns type(C) to a local variable, there
is no difference. The only advantage of PY_NEW is that one has less to
write.

Best regards,
Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to