Ticket #9542: trac_9542-znpoly_timing.patch

File trac_9542-znpoly_timing.patch, 3.7 KB (added by was, 10 years ago)

speeds up multiplication by a factor of *20* for many benchmarks... but causes a segfault when doctesting rings/arith.py

  • sage/misc/all.py

    diff -r bb846fce03db -r 59e8946ca592 sage/misc/all.py
    a b  
    1313
    1414from html import html
    1515
    16 from sage_timeit_class import timeit
     16from sage_timeit_class import timeit, test_timing
    1717
    1818from edit_module import edit, set_edit_template
    1919
  • sage/misc/sage_timeit_class.pyx

    diff -r bb846fce03db -r 59e8946ca592 sage/misc/sage_timeit_class.pyx
    a b  
    8282        print self.eval(code, globals, preparse=preparse, **kwds)
    8383
    8484timeit = SageTimeit()
     85
     86
     87
     88
     89from misc import cputime, walltime
     90import preparser
     91def test_timing(code, max_cputime, max_walltime=0, loops=1):
     92    G = globals()
     93    code0 = code; code = preparser.preparse(code)
     94    t = cputime(); w = walltime()
     95    for i in range(loops):
     96        eval(code, G)
     97    if max_cputime>0 and cputime(t) > max_cputime:
     98        print "CPU time (%s) exceeded evaluating '%s' %s times"%(max_cputime, code0, loops)
     99    if max_walltime>0 and walltime(w)  > max_walltime:
     100        print "WALL time (%s) exceeded evaluating '%s' %s times"%(max_walltime, code0, loops)
     101   
    85102       
    86103
  • sage/rings/polynomial/polynomial_template.pxi

    diff -r bb846fce03db -r 59e8946ca592 sage/rings/polynomial/polynomial_template.pxi
    a b  
    198198        cdef cparent _parent = get_cparent((<Polynomial_template>self)._parent)
    199199        return [self[i] for i in range(celement_len(&self.x, _parent))]
    200200
    201     def __dealloc__(self):
     201    def __del__(self):
    202202        """
    203203        EXAMPLE::
    204204
  • sage/rings/polynomial/polynomial_zmod_flint.pyx

    diff -r bb846fce03db -r 59e8946ca592 sage/rings/polynomial/polynomial_zmod_flint.pyx
    a b  
    3737from sage.structure.factorization import Factorization
    3838from sage.structure.element import coerce_binop
    3939
     40from sage.structure.parent cimport Parent
     41
    4042# We need to define this stuff before including the templating stuff
    4143# to make sure the function get_cparent is found since it is used in
    4244# 'polynomial_template.pxi'.
     
    99101                pass
    100102        Polynomial_template.__init__(self, parent, x, check, is_gen, construct)
    101103
     104    cpdef RingElement _mul_(self, RingElement right):
     105        """
     106        TESTS::
     107
     108            sage: R.<x> = Integers(2^6)[]
     109            sage: f = R([1,2,5,-9]); g = R([1,2,3,4])
     110            sage: f*g
     111            4060*x^6 + 4089*x^5 + 5*x^4 + 11*x^3 + 12*x^2 + 4*x + 1
     112            sage: test_timing('f*g', 1)      # optional -- timing
     113            True
     114        """
     115        cdef Polynomial_zmod_flint r = PY_NEW(Polynomial_zmod_flint)
     116        r._parent = self._parent
     117        zmod_poly_mul(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x)
     118        return r
     119
     120    cpdef ModuleElement _add_(self, ModuleElement right):
     121        cdef Polynomial_zmod_flint r = PY_NEW(Polynomial_zmod_flint)
     122        r._parent = self._parent
     123        zmod_poly_add(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x)
     124        return r
     125   
     126    cpdef ModuleElement _sub_(self, ModuleElement right):
     127        cdef Polynomial_zmod_flint r = PY_NEW(Polynomial_zmod_flint)
     128        r._parent = self._parent
     129        zmod_poly_sub(&r.x, &(<Polynomial_template>self).x, &(<Polynomial_template>right).x)
     130        return r
     131
     132    def __dealloc__(self):
     133        zmod_poly_clear(&self.x)
     134
    102135    cdef Polynomial_template _new(self):
    103136        """
    104137        EXAMPLES::