Well you do know that R is 'interpreted' and that C is 'compiled'. In R you are calling functions to perform the operations. There is a function for indexing ('['):
> get ("[") .Primitive("[") So your 'for' statement in R is interpreting the R code and calling some functions to perform the operations. In "C", the compiler has turned your code into assembly language statements that are performing the operations at a machine level. R is at least one level removed from that. Now when you are performing some vector operations on large objects, some of the code may be close to the speed of C, but you should not expect some statements like you have to really compare between R and C because of the nature of interpreted vs. compiled code. BTW, if you are on a LINUX/UNIX machine, write the same code in a "shell" script and compare the speeds. This may give you an idea of the differences. In the "shell" each of your commands is invoking a C program, executing some code inside the shell. Does this help clarify the differences? On 10/23/07, Walter Alini <[EMAIL PROTECTED]> wrote: > > 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 > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.