#6199: Integer * int is slow
-------------------------------+--------------------------------------------
 Reporter:  fredrik.johansson  |         Owner:  somebody                      
     Type:  defect             |        Status:  needs_info                    
 Priority:  major              |     Milestone:  sage-duplicate/invalid/wontfix
Component:  basic arithmetic   |    Resolution:                                
 Keywords:                     |        Author:                                
 Upstream:  N/A                |      Reviewer:                                
   Merged:                     |   Work_issues:                                
-------------------------------+--------------------------------------------
Changes (by robertwb):

  * status:  new => needs_info


Comment:

 If we're going to special case, we could override {{{__mul__}}} for
 Integer directly, rather than stick stuff in {{{Element.__mul__}}}. That's
 "breaking the rules" even more though. Alternatively, we could add a
 {{{cdef Element _mul_long(self, long n)}}} method which is checked for
 right after the parents aren't the same. This is a bit hackish, but would
 provide a good avenue for improvements for other types as well (QQ, RDF).
 Do we need addition as well? (No, I don't think this should be extended to
 other operands...)

 Note that

 {{{
 sage: timeit("a", number=10**6)
 1000000 loops, best of 3: 58.1 ns per loop
 }}}

 so I wrote my own

 {{{
 def time_mul(a, b, long n):
     cdef long i
     for i in range(n):
         a*b
 }}}

 With this, I did a special _mul_long method.

 Before:
 {{{
 sage: a = 123 ; b = 456 ; c = 456r ; y = RDF(pi)
 sage: a*c, a*y
 (56088, 386.415896392)
 sage: %time time_mul(a,b, 10**7)
 CPU times: user 1.13 s, sys: 0.00 s, total: 1.13 s
 Wall time: 1.13 s
 sage: %time time_mul(a,c, 10**7)
 CPU times: user 17.14 s, sys: 0.04 s, total: 17.18 s
 Wall time: 17.25 s
 sage: %time time_mul(a,y, 10**7)
 CPU times: user 8.21 s, sys: 0.02 s, total: 8.24 s
 Wall time: 8.25 s
 }}}

 After:
 {{{
 sage: a = 123 ; b = 456 ; c = 456r ; y = RDF(pi)
 sage: a*c, a*y
 (56088, 386.415896392)
 sage: %time time_mul(a,b, 10**7)
 CPU times: user 1.16 s, sys: 0.00 s, total: 1.16 s
 Wall time: 1.17 s
 sage: %time time_mul(a,c, 10**7)
 CPU times: user 0.95 s, sys: 0.00 s, total: 0.96 s
 Wall time: 0.96 s
 sage: %time time_mul(a,y, 10**7)
 CPU times: user 8.52 s, sys: 0.02 s, total: 8.54 s
 Wall time: 8.57 s
 }}}

 I'm leaning towards it being worth it, on the right only, for * and +
 only. The cost is (on my machine) an extra 25ns per non-identical-parents
 operation, which are at the cheapest is at least 100s of ns.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/6199#comment:6>
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