From maechler at stat.math.ethz.ch  Wed Oct 30 11:35:32 2013
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Wed, 30 Oct 2013 11:35:32 +0100
Subject: [Rd] Imports, importFrom slow (for Matrix)
In-Reply-To: <CABtg=Km0PF9hRFXShDcm=m2vPnyg4Lm5BJ3JAwVW=YwQQRZc3A@mail.gmail.com>
References: <CABtg=KmyKQCp3QBvrar76mF0zUMNayRjT8PtOrKZrmOtAWrS9w@mail.gmail.com>
	<526FC1FA.60200@stats.ox.ac.uk>
	<CABtg=KnprQ0HeK3vK2=6X4HpZ99NjPu365fX+wZUuQPnbL2NuA@mail.gmail.com>
	<CABtg=Km0PF9hRFXShDcm=m2vPnyg4Lm5BJ3JAwVW=YwQQRZc3A@mail.gmail.com>
Message-ID: <21104.57588.159866.762076@stat.math.ethz.ch>

>>>>> G?bor Cs?rdi <csardi.gabor at gmail.com>
>>>>>     on Tue, 29 Oct 2013 10:31:14 -0400 writes:

    > Oh, you mean to put Matrix:: in the functions that need
    > Matrix, right, of course. Then yes, this could be a
    > solution. I have some issue with some new class
    > definitions, but I can probably work them out.

    > Gabor

otherwise, please contact  Matrix-authors at r-project.org 
(me being one of the two).

To the whole issue in the 'Subject':  Yes, indeed importing from
Matrix i.e., loading the Matrix namespace, is slow notably compared to many other parts of R including its startup.

I have not had time to investigate, but I even have a vague
feeling that it got considerably slower than it used to be a
couple of R (and Matrix) versions ago.

Note that your timings below are a bit biased because you use
Rscript which unfortunately does not pre-[load + attach] the
'methods' package.
But your bias is only about 0.1 second from my measurements;
the time increases from 0.35 to 0.45  not sure if this is still
a good enough reason to omit  'methods' from Rscript.

Martin



    > On Tue, Oct 29, 2013 at 10:25 AM, G?bor Cs?rdi
    > <csardi.gabor at gmail.com> wrote:
    >> Unfortunately that seems to be (almost) just as slow.
    >> 
    >> ~$ time Rscript -e 'Matrix::summary; ls()' > /dev/null
    >> real 0m2.785s user 0m2.668s sys 0m0.112s
    >> 
    >> Gabor
    >> 
    >> On Tue, Oct 29, 2013 at 10:11 AM, Prof Brian Ripley
    >> <ripley at stats.ox.ac.uk> wrote:
    >>> On 29/10/2013 14:03, G?bor Cs?rdi wrote:
    >>>> 
    >>>> Dear All,
    >>>> 
    >>>> before its latest version my package had 'Imports:
    >>>> Matrix' in its DESCRIPTION file, but it did not import
    >>>> anything in NAMESPACE. Rather, some functions
    >>>> explicitly loaded Matrix, as they needed. The reason
    >>>> for this was that importing Matrix is really slow, and
    >>>> only very few igraph functions need it. (I guess Matrix
    >>>> is slow because of the many registered names, but that
    >>>> is another question.)
    >>>> 
    >>>> # Empty session: ~$ time Rscript -e 'ls()' > /dev/null
    >>>> real 0m0.251s user 0m0.196s sys 0m0.049s
    >>>> 
    >>>> # Without importing from Matrix: ~$ time Rscript -e
    >>>> 'library(igraph); ls()' > /dev/null Loading required
    >>>> package: methods real 0m0.419s user 0m0.363s sys
    >>>> 0m0.049s
    >>>> 
    >>>> # Adding importFrom(Matrix, sparseMatrix) to NAMESPACE:
    >>>> ~$ time Rscript -e 'library(igraph); ls()' > /dev/null
    >>>> Loading required package: methods real 0m2.963s user
    >>>> 0m2.844s sys 0m0.115s
    >>>> 
    >>>> This solution was fine with me, especially because
    >>>> other packages depending on igraph and using Matrix
    >>>> through igraph worked fine on the CRAN build servers,
    >>>> as igraph brought Matrix with it. (The build servers
    >>>> don't have recommended packages like Matrix available
    >>>> by default.)
    >>>> 
    >>>> Recently, R CMD check does not allow me to list Matrix
    >>>> at Imports without importing something from it. This is
    >>>> understandable, because it is an inconsistency after
    >>>> all, but caused some headache for me.
    >>>> 
    >>>> A 3s loading time for a package is IMHO much longer
    >>>> than ideal, especially that loading R itself is ten
    >>>> times faster. So I definitely don't want to import from
    >>>> Matrix right now.
    >>>> 
    >>>> The solution I settled with was to include Matrix in
    >>>> 'Suggests', and the load it selectively, as before. Now
    >>>> some packages depending on igraph are failing on the
    >>>> CRAN build servers, which don't have Matrix installed
    >>>> for these packages. (Luckily they are probably not
    >>>> failing for users, because most users do have the
    >>>> recommended packages.)
    >>>> 
    >>>> In summary, it would be great to speed up imports.
    >>>> 
    >>>> Another solution would be some mechanism that allows me
    >>>> to import from a package as needed, not at the package
    >>>> loading time. Something like a delayed importFrom().
    >>> 
    >>> 
    >>> That is what Matrix:: does.  There is nothing like
    >>> enough here for us to tell why it would not suffice for
    >>> you.  (If you want to import something occasionally and
    >>> then use it very many times, make a local copy.)
    >>> 
    >>>> 
    >>>> Just wanted to bring up this issue.
    >>>> 
    >>>> Best, Gabor
    >>>> 
    >>>> ______________________________________________
    >>>> R-devel at r-project.org mailing list
    >>>> https://stat.ethz.ch/mailman/listinfo/r-devel
    >>>> 
    >>> 
    >>> 
    >>> --
    >>> Brian D. Ripley, ripley at stats.ox.ac.uk Professor of
    >>> Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
    >>> University of Oxford, Tel: +44 1865 272861 (self) 1
    >>> South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG,
    >>> UK Fax: +44 1865 272595
    >>> 
    >>> ______________________________________________
    >>> R-devel at r-project.org mailing list
    >>> https://stat.ethz.ch/mailman/listinfo/r-devel

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Imports, importFrom slow (for Matrix)
Martin Maechler