[R] The L Word

Martin Maechler maechler at stat.math.ethz.ch
Thu Feb 24 16:45:27 CET 2011


>>>>> "CB" == Claudia Beleites <cbeleites at units.it>
>>>>>     on Thu, 24 Feb 2011 12:31:55 +0100 writes:

    CB> On 02/24/2011 11:20 AM, Prof Brian Ripley wrote:
    >> On Thu, 24 Feb 2011, Tal Galili wrote:
    >> 
    >>> Thank you all for the answers.
    >>> 
    >>> So if I may extend on the question -
    >>> When is it important to use 'Literal integer'?
    >>> Under what situations could not using it cause problems?
    >>> Is it a matter of efficiency or precision or both?
    >> 
    >> Efficiency: it avoids unnecessary type conversions. For example
    >> 
    >> length(x) > 1
    >> 
    >> has to coerce the lhs to double. We have converted the base
    >> code to use integer constants because such small efficiency
    >> gains can add up.
    >> 
    >> Integer vectors can be stored more compactly than doubles, but
    >> that is not going to help for length 1:
    >> 
    >>> object.size(1)
    >> 48 bytes
    >>> object.size(1L)
    >> 48 bytes
    >> (32-bit system).
    CB> see:

    CB> n <- 0L : 100L

    CB> szi <- sapply (n, function (n) object.size (integer (n)))
    CB> szd <- sapply (n, function (n) object.size (double (n)))
    CB> plot (n, szd)
    CB> points (n, szi, col = "red")

yes. 

Note however that I've never seen evidence for a *practical*
difference in simple cases, and also of such cases as part of a
larger computation.
But I'm happy to see one if anyone has an interesting example.

E.g., I would typically never use  0L:100L  instead of 0:100
in an R script because I think code readability (and self
explainability) is of considerable importance too.

Martin



More information about the R-help mailing list