[Numpy-discussion] import overhead of numpy.testing

Ralf Gommers ralf.gommers at gmail.com
Sat Aug 10 04:28:47 EDT 2013


On Wed, Aug 7, 2013 at 3:26 PM, Andrew Dalke <dalke at dalkescientific.com>wrote:

> On Aug 7, 2013, at 4:37 AM, Charles R Harris wrote:
> > I haven't forgotten and intend to look at it before the next release.
>
> Thanks!
>
> On a related topic, last night I looked into deferring the
> import for numpy.testing. This is the only other big place
> where numpy's import overhead might be reduced without
> breaking backwards compatibility.
>

It does break backwards compatibility though, because now you can do:

    import numpy as np
    np.testing.assert_equal(x, y)



> I made a _DeferredTester [1] and replaced the 10 __init__.py
> uses of:
>
> from .testing import Tester
> test = Tester().test
> bench = Tester().bench
>
> to use the _DeferredTester instead.
>
> With that in place the "import numpy" time (best of 5)
> goes from 0.0796 seconds to 0.0741 seconds, or 7%.
>
> That 0.0796 includes the 0.02 seconds for the exec()
> of the polynomial templates. Without that 0.02 seconds
> in the baseline would give a 10% speedup. [2]
>
> Would this sort of approach be acceptable to NumPy?
> If so, I could improve the patch to make it be acceptable.
>

I think if it's tested well, implementing np.test() with a deferred tester
is OK imho.

However, I would not be happy with breaking backwards compatibility for the
numpy.testing module. Do you have more detailed timings? I'm guessing the
bottleneck is importing nose. If so, you can still import numpy.testing
into the numpy namespace without losing your gain in import time (nose is
an optional dependency, so not imported by default inside numpy.testing).


> The outstanding code issues to be resolve before making
> a pull request are:
>
> 1) The current wrapper uses *args and **kwargs to forward
>    any test() and bench() calls to the actual function.
>    As a result, parameter introspection doesn't work.
>
> 2) The current wrapper doesn't have a __doc__
>
> 3) The only way to fix 1) and 2) is to copy the signatures
>    and docstring from the actual Tester() implementation,
>    which causes code/docstring duplication.
>

That all sounds fixable.


>
> 4) I don't know if it's appropriate to add my _DeferredTester
>    to numpy.core vs. some other place in the code base.
>

numpy.lib I'd think.


> If you want to see the patch, I followed the NumPy instructions at
>  http://docs.scipy.org/doc/numpy/dev/gitwash/git_development.html
> and made an experimental fork at
>  https://github.com/adalke/numpy/tree/no-default-tester-import
>
> I have no git/github experience beyond what I did for this
> patch, so let me know if there are problems in what I did.
>

You did everything correctly.

Cheers,
Ralf

P.S. I also see some unused imports and files. Will send a cleanup PR.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130810/48d060ad/attachment.html>


More information about the NumPy-Discussion mailing list