By the way, I looked at the help page for digits,
sage: base=7
sage: x=12345654321234565432123456543212345654321234565432123456
sage: x.digits?
and it is said there (at the end) that
" Using ``sum()`` and ``enumerate()`` to do the same thing is
slightly faster in many cases (and ``balanced_sum()`` may be
faster
yet). Of course it gives the same result:
sage: base = 4
sage: sum(digit * base^i for i, digit in
enumerate(x.digits(base))) == ZZ(x.digits(base), base)
True
"
However, timing shows that ZZ is faster than sum with enumerate, and
reduce is faster than both of them,
sage: L=x.digits(base)
sage: timeit('sum(digit * base^i for i, digit in enumerate(L))')
625 loops, best of 3: 59.2 ��s per loop
sage: timeit('ZZ(L,base)')
625 loops, best of 3: 57.3 ��s per loop
sage: timeit('reduce(lambda x,y:base*x+y,reversed(L),0)')
625 loops, best of 3: 29.2 ��s per loop
Alec Mihailovs
--
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
To unsubscribe from this group, send email to
sage-devel+unsubscribegooglegroups.com or reply to this email with the words
"REMOVE ME" as the subject.