Para estudiar qué tan eficiente es algún código, conviene saber evaluar cuánta memoria RAM requiere y cuánto tarda su ejecución.
> n <- 2^15
> M <- matrix(runif(n * n), ncol=n, nrow=n)
> system.time(min(M))
user system elapsed
0.982 0.001 0.984
> install.packages("pryr")
also installing the dependencies ‘stringi’, ‘stringr’, ‘Rcpp’
trying URL 'https://cran.itam.mx/bin/macosx/mavericks/contrib/3.3/stringi_1.1.5.tgz'
Content type 'application/x-gzip' length 12650570 bytes (12.1 MB)
==================================================
downloaded 12.1 MB
trying URL 'https://cran.itam.mx/bin/macosx/mavericks/contrib/3.3/stringr_1.2.0.tgz'
Content type 'application/x-gzip' length 139879 bytes (136 KB)
==================================================
downloaded 136 KB
trying URL 'https://cran.itam.mx/bin/macosx/mavericks/contrib/3.3/Rcpp_0.12.11.tgz'
Content type 'application/x-gzip' length 3114233 bytes (3.0 MB)
==================================================
downloaded 3.0 MB
trying URL 'https://cran.itam.mx/bin/macosx/mavericks/contrib/3.3/pryr_0.1.2.tgz'
Content type 'application/x-gzip' length 501137 bytes (489 KB)
==================================================
downloaded 489 KB
The downloaded binary packages are in
/var/folders/qj/1gw209fx1qb31lz61njz69k00000gn/T//RtmphnuSlq/downloaded_packages
> library(pryr)
> n <- 2^15
> M <- matrix(runif(n * n), ncol=n, nrow=n)
> object_size(M)
8.59 GB
Una vez que un objeto pesado ya no se requiere, se
puede eliminar con la instrucción rm()
y
así liberar memoria para agilizar la ejecución de
operaciones futuras. Para forzar la liberación
de memoria que ya cuenta con referencias desde variables existentes,
se puede solicitar la ejecución del recolector de
basura con gc()
.
https://satuelisa.github.io/simulation/medicion/R.html