[R] Replace values on seq

Walter Alini walteralini at gmail.com
Tue Oct 23 23:40:49 CEST 2007


> code (C in this case).  What exactly are you asking?  You can alway
> 'time' (sys.time) a set of statements to see which is better (just
> make sure you execute them enough times to get reasonable readings --
> several seconds)

I was wondering where to seach for the code of these different options
to realize where the differences between them are.

The fact is that I have implemented some code to change an image (big
matrix), by calling .C and the for I mentioned in this first mail:
(a)
for (i in 1:length(data)){
       data[i] <- table[data[i]+1]
}

My C function performs practically the same:
(b)
for (i = 0; i < size; i++){
	image[i] = table[image[i]];
}

and I used Rprof to discover that, by running both of these functions,
one after the other, the first one (a) uses 99.6 aprox of the CPUs
time and (b) the other 0.4

So, the first I asked is to improve (a) which I got yours answers (and
will test these days).

My question is, why is (b) so much faster than (a) (and I think also
faster than the solution you guys gave to me, but that is my opinion)?

Hope this is clear, (I managed my poor English as well as I could)
Thanks again,
Walter



More information about the R-help mailing list