[Rd] Best practices for writing R functions (really copying)

2011-07-25 Thread Radford Neal
Gabriel Becker writes: AFAIK R does not automatically copy function arguments. R actually tries very hard to avoid copying while maintaining pass by value functionality. ... R only copies data when you modify an object, not when you simply pass it to a function. This is a bit

Re: [Rd] Best practices for writing R functions (really copying)

2011-07-25 Thread Matt Shotwell
Also consider subsetting: cat(a: ); print(system.time( { A - matrix(c(1.0,1.1),5,1000); 0 } )) cat(h: ); print(system.time( { sum(A[1:5,1:1000]) } )) cat(i: ); print(system.time( { sum(A[]) } )) cat(j: ); print(system.time( { sum(A) } )) In contrast with Python's NumPy array, the R array

Re: [Rd] Best practices for writing R functions (really copying)

2011-07-25 Thread Henrik Bengtsson
Use tracemem() instead, i.e. A - matrix(c(1.0,1.1), nrow=5, ncol=10); tracemem(A); [1] 0x047ab170 A[1,1] - 7; B - sqrt(A); tracemem[0x047ab170 - 0x0552f338]: A[1,1] - 7; B - t(A); A[1,1] - 7; tracemem[0x047ab170 - 0x057ba588]: A[1,1] - 7; A[1,1] -