Make mod_int signed?

36 views
Skip to first unread message

Volker Braun

unread,
May 21, 2013, 7:40:00 AM5/21/13
to sage-...@googlegroups.com
Reposted at the top to increase visiblitiy (see https://groups.google.com/d/msg/sage-devel/5PdRIUic2Es/l0yDYLG_8qIJ)

The sage matrices use unsigned long for the mod_int datatype. This does not fit into a (signed) python int, so whenever it reaches Python (assigning to a Python variable, or passing as an argument to a non-cdef function) it gets converted to a Python arbitrary-precision integer. This wastes quite a bit of time when creating it and gets worse once you start doing arithmetic:

--------------------------------------
ctypedef unsigned long mod_int   # this is how we define it

cpdef caster_slow():
    cdef int i
    foo = None
    for i in range(10000000):
        foo = <mod_int>(1.0)

cpdef caster_fast():
    cdef int i
    foo = None
    for i in range(10000000):
        foo = <int>(1.0)
--------------------------------------

Then we get

sage: timeit('caster_slow()')
5 loops, best of 3: 137 ms per loop
sage: timeit('caster_fast()')
25 loops, best of 3: 34.4 ms per loop

Should we just switch to (signed) long? This wastes one bit (factor of 2) of maximum modulus length, but at least for now we define MAX_MODULUS = 2**23 so this wouldn't be an issue.


Martin Albrecht

unread,
May 21, 2013, 8:47:07 AM5/21/13
to sage-...@googlegroups.com
Sounds like a good idea to me.
Cheers,
Martin

--
name: Martin Albrecht
_pgp: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x8EF0DC99
_otr: 47F43D1A 5D68C36F 468BAEBA 640E8856 D7951CCF
_www: http://martinralbrecht.wordpress.com/
_jab: martinr...@jabber.ccc.de
signature.asc

Jean-Pierre Flori

unread,
May 21, 2013, 9:02:15 AM5/21/13
to sage-...@googlegroups.com


On Tuesday, May 21, 2013 2:47:07 PM UTC+2, Martin Albrecht wrote:
Sounds like a good idea to me.
Sounds good to me too.

Mike Hansen

unread,
May 21, 2013, 9:05:52 AM5/21/13
to sage-devel
I would just make sure that the unpickling old matrices still works.

--Mike
--Mike
> --
> 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+...@googlegroups.com.
> To post to this group, send email to sage-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-devel?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Volker Braun

unread,
May 22, 2013, 4:10:40 PM5/22/13
to sage-...@googlegroups.com
This is now http://trac.sagemath.org/14627  (needs review)
Reply all
Reply to author
Forward
0 new messages