#11589: faster zero matrix creation
------------------------------+---------------------------------------------
   Reporter:  malb            |          Owner:  jason, was                 
       Type:  enhancement     |         Status:  new                        
   Priority:  major           |      Milestone:  sage-4.7.2                 
  Component:  linear algebra  |       Keywords:                             
Work_issues:                  |       Upstream:  N/A                        
   Reviewer:                  |         Author:  Martin Albrecht, Simon King
     Merged:                  |   Dependencies:                             
------------------------------+---------------------------------------------

Comment(by SimonKing):

 I was wrong in assuming that `entries=0` would provoke a second
 initialisation of the diagonal.

 However, using None as default argument does have a tiny advantage: The
 test `if entries is None or entries==0` is a tad faster if it is actually
 None.

 With `entries=None` as default in the `__call__` method of matrix spaces,
 timings do improve, and I obtain a threshold `nrows==ncols==40`: For
 larger matrices, over GF(p) with p>2, creation from scratch is faster,
 while for smaller matrices copying is faster.

 However, with the original patch, I find a big surprise: Over GF(2),
 copying is ''always'' faster! For GF(p), p>2, I can confirm your statement
 that the threshold seems to be around 100.

 There is one problem with default `entries=None`, though: Some matrix
 classes (used for matrices over the reals or symbolics) don't accept
 `entries=None`. So, their init methods should be adapted as well.

 Question: Is it worth it?

 Using None, I obtain for example:
 {{{
 sage: timeit("M = matrix(GF(2),30,30)", number=10000)
 10000 loops, best of 3: 105 µs per loop
 sage: timeit("M = matrix(GF(2),300,300)", number=10000)
 10000 loops, best of 3: 107 µs per loop
 sage: timeit("M = matrix(GF(3),30,30)", number=10000)
 10000 loops, best of 3: 102 µs per loop
 sage: timeit("M = matrix(GF(3),300,300)", number=10000)
 10000 loops, best of 3: 210 µs per loop
 }}}

 Using 0, I obtain:
 {{{
 sage: timeit("M = matrix(GF(2),30,30)", number=10000)
 10000 loops, best of 3: 113 µs per loop
 sage: timeit("M = matrix(GF(2),300,300)", number=10000)
 10000 loops, best of 3: 123 µs per loop
 sage: timeit("M = matrix(GF(3),30,30)", number=10000)
 10000 loops, best of 3: 102 µs per loop
 sage: timeit("M = matrix(GF(3),300,300)", number=10000)
 10000 loops, best of 3: 215 µs per loop
 }}}

 The difference is small, but it seems quite reproducible (not flaky) for
 me. So, my suggestion is to go through the matrix classes and allow
 `entries=None`.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/11589#comment:8>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to