[R] Integer vs numeric
(Ted Harding)
Ted.Harding at manchester.ac.uk
Tue Jan 29 00:34:57 CET 2008
On 28-Jan-08 22:40:02, Peter Dalgaard wrote:
> [...]
> AFAIR, space is/was more of an issue. If you do something like
>
> for i in 1:1e7
> some.silly.simulation()
>
> then you have 40 MB sitting there doing nothing, and 80 MB if
> it had been floating point.
Hmmm ... there's something to be said for good old
for(i=1,i<=1e7,i++){....}
As pointed out in ?"for", when you do
for(i in X){...} #(e.g. X=(1:1e7))
the object X is created (or is already there) in full
at the start and sits there, as you say doing nothing,
until you end the loop. Whereas the C code just keeps
track of i and of the condition.
At least on a couple of my machines (64MB and 184MB RAM)
knocking out 40MB would inflict severe trauma! Let alone 80MB.
Mind you, the little one is no longer allowed to play with
big boys like R, though the other one is still used for
moderate-sized games.
Would there be much of a time penalty in implementing
a 'for' loop, C-style, as
i<-1
while(i<=1e7){
...
i<-i+1
}
??
It looks as though there might be:
system.time(for(i in (1:1e7)) x<-cos(3) )
#[1] 13.521 0.132 13.355 0.000 0.000
system.time({i<-1;while(i<=1e7){x<-cos(3);i<-i+1}})
#[1] 38.270 0.076 37.629 0.000 0.000
which suggests that the latter is about 3 times as slow.
(And no, this wasn't done on either of my puny babes).
(And this isn't the first time I've wished for an R
implementation of "++" as a CPU-level incrementation,
as opposed to the R-arithmetic implementation which
treats "adding 1 to a variable" as a full-dress
arithmetic parade!
Best wishes,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 28-Jan-08 Time: 23:34:52
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list