Status: Started
Owner: asmeurer
CC: mattpap
Labels: Type-Defect Priority-Medium NeedsReview Polynomial

New issue 1883 by asmeurer: Recode dmp_zero_p to be non-recursive
http://code.google.com/p/sympy/issues/detail?id=1883

A simple cProfile.run('integrate(x**2*exp(x)*sin(x), x)', sort=1)
reveals that most of the time is spent in dmp_zero_p for this integral.
However, it was coded recursively, when it is trivial to code it
non-recursively. This makes it faster:

In [11]: def rec_dmp_zero_p(f, u): # this is the old version
    if not u:
        return not f
    else:
        if len(f) == 1:
            return rec_dmp_zero_p(f[0], u - 1)
        else:
   ....:             return False
   ....:
   ....:

In [18]: def non_rec_dmp_zero_p(f, u): # this is the new version
   ....:     while u:
   ....:         if len(f) != 1:
   ....:             return False
   ....:         f = f[0]
   ....:         u -= 1
   ....:     return not f
   ....:

In [19]: f =
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[-3]]]]]]]]]]]]]]]]]]]]]]]]]
]]]]]]]]]]]]]]]]]]]]]]

In [24]: u = 46

In [25]: rec_dmp_zero_p(f, u)
Out[25]: False

In [26]: non_rec_dmp_zero_p(f, u)
Out[26]: False

In [27]: g =
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]
]]]]]]]]]]]]]]]]]]]]

In [28]: rec_dmp_zero_p(g, u)
Out[28]: True

In [29]: non_rec_dmp_zero_p(g, u)
Out[29]: True

In [30]: %timeit rec_dmp_zero_p(f, u)
10000 loops, best of 3: 23.8 us per loop

In [31]: %timeit non_rec_dmp_zero_p(f, u)
100000 loops, best of 3: 14.3 us per loop

In [32]: %timeit rec_dmp_zero_p(g, u)
10000 loops, best of 3: 22.8 us per loop

In [33]: %timeit non_rec_dmp_zero_p(g, u)
100000 loops, best of 3: 15.1 us per loop

And in the end, we find that the integral runs a little faster:

Before:

In [1]: %timeit integrate(x**2*exp(x)*sin(x), x)
1 loops, best of 3: 7.49 s per loop

After:

In [1]: %timeit integrate(x**2*exp(x)*sin(x), x)
1 loops, best of 3: 6.27 s per loop

Fix is at http://github.com/asmeurer/sympy/tree/non-rec-dmp_zero_p. Please review.

--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

--
You received this message because you are subscribed to the Google Groups 
"sympy-issues" 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/sympy-issues?hl=en.

Reply via email to