25

I have noticed a curious thing whilst working in R. When I have a simple program that computes squares from 1 to N implemented using for-loop and while-loop the behaviour is not the same. (I don't care about vectorisation in this case or apply functions).

fn1 <- function (N) 
{
    for(i in 1:N) {
        y <- i*i
    }
}

AND

fn2 <- function (N) 
{
    i=1
    while(i <= N) {
        y <- i*i
        i <- i + 1
    }
}

The results are:

system.time(fn1(60000))
   user  system elapsed 
  2.500   0.012   2.493 
There were 50 or more warnings (use warnings() to see the first 50)
Warning messages:
1: In i * i : NAs produced by integer overflow
.
.
.

system.time(fn2(60000))
   user  system elapsed 
  0.138   0.000   0.137 

Now we know that for-loop is faster, my guess is because of pre allocation and optimisations there. But why does it overflow?

UPDATE: So now trying another way with vectors:

fn3 <- function (N) 
{
    i <- 1:N
    y <- i*i
}
system.time(fn3(60000))
   user  system elapsed 
  0.008   0.000   0.009 
Warning message:
In i * i : NAs produced by integer overflow

So Perhaps its a funky memory issue? I am running on OS X with 4Gb of memory and all default settings in R. This happens in 32- and 64-bit versions (except that times are faster).

Alex

4
  • 6
    Based on your timing while-loop is faster.
    – Marek
    Nov 17, 2010 at 11:38
  • 2
    when you convert the counter in the for loop to a float it will be faster than the while loop but it's only because the for loop then has no warnings.
    – John
    Nov 17, 2010 at 13:13
  • 1
    R is full of this sort of nonsense.
    – Alex Brown
    Nov 17, 2010 at 13:34
  • Good question, though. I like the performance analysis.
    – Alex Brown
    Nov 19, 2010 at 8:17

3 Answers 3

37

Because 1 is numeric, but not integer (i.e. it's a floating point number), and 1:6000 is numeric and integer.

> print(class(1))
[1] "numeric"
> print(class(1:60000))
[1] "integer"

60000 squared is 3.6 billion, which is NOT representable in signed 32-bit integer, hence you get an overflow error:

> as.integer(60000)*as.integer(60000)
[1] NA
Warning message:
In as.integer(60000) * as.integer(60000) : NAs produced by integer overflow

3.6 billion is easily representable in floating point, however:

> as.single(60000)*as.single(60000)
[1] 3.6e+09

To fix your for code, convert to a floating point representation:

function (N)
{
    for(i in as.single(1:N)) {
        y <- i*i
    }
}
3
  • 2
    is that why seq(N) is preferred over 1:N ? Nov 19, 2010 at 7:30
  • 1
    @Marek: Thought so too, but that gives an integer vector. Exactly what we didn't want. seq(1,N,1) gives a numeric vector.
    – Joris Meys
    Nov 19, 2010 at 11:44
  • @David : it's a matter of choice. Both are equivalent, and both give a vector of integers. seq(1,N,1) is the equivalent of as.single(1:N). See the answer of Spacedman as well.
    – Joris Meys
    Nov 19, 2010 at 11:45
4

The variable in the for loop is an integer sequence, and so eventually you do this:

> y=as.integer(60000)*as.integer(60000)
Warning message:
In as.integer(60000) * as.integer(60000) : NAs produced by integer overflow

whereas in the while loop you are creating a floating point number.

Its also the reason these things are different:

> seq(0,2,1)
[1] 0 1 2
> seq(0,2)
[1] 0 1 2

Don't believe me?

> identical(seq(0,2),seq(0,2,1))
[1] FALSE

because:

> is.integer(seq(0,2))
[1] TRUE
> is.integer(seq(0,2,1))
[1] FALSE
3
  • But why would floating points have larger range than integers?
    – Alex
    Nov 17, 2010 at 10:45
  • 1
    UPDATE: "Note that on almost all implementations of R the range of representable integers is restricted to about +/-2*10^9: doubles can hold much larger integers exactly." From R documentation for integer :(
    – Alex
    Nov 17, 2010 at 10:55
  • @Alex (not Brown): an integer is a binary representation in 32bits. So the maximum range is 2^32, or ~ [-2e9, +2e9]. A floating point uses the 32 bits different : it saves a number of bits for the digits (where the decimal is after the first digit), and another set of bits for the power. Obviously this makes the range very much larger. This is general computing knowledge by the way, apart from some small details this system is the same in every computer language / application.
    – Joris Meys
    Nov 17, 2010 at 13:31
3

And about timing:

fn1 <- function (N) {
    for(i in as.numeric(1:N)) { y <- i*i }
}
fn2 <- function (N) {
    i=1
    while (i <= N) {
        y <- i*i
        i <- i + 1
    }
}

system.time(fn1(60000))
# user  system elapsed 
# 0.06    0.00    0.07 
system.time(fn2(60000))
# user  system elapsed 
# 0.12    0.00    0.13

And now we know that for-loop is faster than while-loop. You cannot ignore warnings during timing.

2
  • 1
    This is still not fully fair since while loop have larger body; this is necessary to emulate for, I know, but in some problems this is not the case.
    – mbq
    Nov 17, 2010 at 14:05
  • 2
    @mbq That why for-loop and while-loop cannot be compare. Each has different purpose. You can add i<-i+1 line in fn1 but it's still be faster cause fn2 has to check condition, which means 60k calls to <=. If you add another line i<=N to fn1 then timings are equal.
    – Marek
    Nov 17, 2010 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.