An S4 class based on an array, but with a fixed number of dimensions, requires overloading the '[' method so that drop=FALSE even when single elements are selected in one of them.
This works well, but once the S4 object is saved to a file and then loaded, the speed drops greatly (20 times larger, code below), while memory usage increases from 0 to 2.5 Mb. What could be causing this? Is there anything that can be done to avoid this penalty? Is the difference in timing between array and the S4 class just due to the S4 system? Using callNextMethod() inside the '[' method made it faster but not if called with all arguments specified, as needed to override the drop=TRUE default. Are there any suggestions of a better mechanism than the call to [email protected] <- [email protected][i, j, k, l, m, n, drop=FALSE] the method is using? Many thanks, Iago # Set farray class, a 6D array setClass("farray", representation("array"), prototype(array(as.numeric(NA), dim=c(1,1,1,1,1,1))) ) # Overload '[' to ensure drop=FALSE setMethod("[", signature(x="farray"), function(x, i, j, k, l, m, n, ..., drop=FALSE) { [email protected] <- [email protected][i, j, k, l, m, n, drop=FALSE] return(x) } ) # Example array arr <- array( runif(prod(c(10, 20, 2, 4, 2, 100))), dim = c(10, 20, 2, 4, 2, 100), dimnames = list(a=seq(10), b=seq(20), c=seq(2), d=seq(4), e=seq(2), f=seq(100)) ) # Example farray farr <- new('farray', arr) # Save farr saveRDS(farr, file='farr.rds') # Check speed library(microbenchmark) library(bench) # array microbenchmark(arr[1,1,1,1,1,1], times=500L, unit='us') bench::mark(arr[1,1,1,1,1,1]) # remove and load rm(farr) farr <- readRDS('farr.rds') microbenchmark(farr[1,1,1,1,1,1], times=500L, unit='us') bench::mark(farr[1,1,1,1,1,1]) -- Iago Mosqueira Wageningen Marine Research Haringkade 1 Postbus 68 1976CP, IJmuiden The Netherlands Tel.: +31 (0)317 488 995 [email protected] ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
