Swen,

Below is what I get when simulating what you do, using four different
functions, three of which use raster objects. This suggests that the speed
of the different approaches does indeed vary but not by as much as you
reported. I find that a function using 'raster' is as fast or faster than
using read.asciigrid, but there might be better approaches out there. This
may or may not hold for your data, OS, settings, which is why it is
important to report the details (and provide reproducible results).

Robert


library(maptools)
library(raster)
library(rgdal)

# generate ascii grids
path <- 'd:/asc/asc'

set.seed(0)
r <- raster(nrow=90, ncol=180)
for (i in 1:25) {
r[] <- runif(ncell(r))
r <- writeRaster(r, paste(path, i, '.asc', sep=''))
}

# filenames
f <- list.files('d:/asc/', full=T)

# raster approach
fun1a <- function(f) {
s <- stack(f)
calc(s, mean)
}

# this is faster on the raster-development version
# but probably not for you
fun1b <- function(f) {
s <- stack(f)
mean(s)
}

# optimizing for speed
# avoiding rgdal overhead, and reading all values into memory
fun1c <- function(f) {
lst <- lapply(f, function(x) getValues(raster(x, native=TRUE)))
m <- do.call(cbind, lst)
r <- raster(f[1]);
setValues(r, rowMeans(m))
}


# using read.asciigrid, probably much faster and
# certainly much shorter than your script
fun2 <- function(f) {
lst <- lapply(f, function(x) read.asciigrid(x)@data )
m <- do.call(cbind, lst)
r <- read.asciigrid(f[1]);
r@data[,1] <- rowMeans(m)
r
}



system.time(x1 <- fun1a(f))
system.time(x2 <- fun1b(f))
system.time(x3 <- fun1c(f))
system.time(y <- fun2(f))

I get:

rep1
> system.time(x1 <- fun1a(f))
   user  system elapsed
   8.69    0.86    9.62
> system.time(x2 <- fun1b(f))
   user  system elapsed
   5.48    0.34    5.96
> system.time(x3 <- fun1c(f))
   user  system elapsed
   3.70    0.08    3.86
> system.time(y <- fun2(f))
   user  system elapsed
   8.20    0.33    8.57
>


rep 2
> system.time(x1 <- fun1a(f))
   user  system elapsed
   7.83    0.92    8.75
> system.time(x2 <- fun1b(f))
   user  system elapsed
   5.21    0.26    5.48
> system.time(x3 <- fun1c(f))
   user  system elapsed
   3.48    0.06    3.56
> system.time(y <- fun2(f))
   user  system elapsed
   9.61    0.57   10.17
>

rep 3
> system.time(x1 <- fun1a(f))
   user  system elapsed
   8.29    0.89    9.17
> system.time(x2 <- fun1b(f))
   user  system elapsed
   5.49    0.35    5.84
> system.time(x3 <- fun1c(f))
   user  system elapsed
   3.84    0.06    3.90
> system.time(y <- fun2(f))
   user  system elapsed
   8.01    0.58    8.58
>



> sessionInfo()
R version 2.13.2 (2011-09-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] rgdal_0.7-1     raster_1.9-35   maptools_0.8-10 lattice_0.19-33
[5] sp_0.9-91       foreign_0.8-46

loaded via a namespace (and not attached):
[1] grid_2.13.2




On Mon, Oct 31, 2011 at 9:49 AM, Robert J. Hijmans <r.hijmans@gmail.com>wrote:

> Swen, That is interesting, can you share some more information about your
> grids (how many columns and rows)? And some info on your platform &
> versions, sessionInfo(). please. As-is your report could be rather
> misleading. And, again, you need not use 'brick'.  Robert
>
>
> On Mon, Oct 31, 2011 at 9:02 AM, Swen Meyer <s.meyer@lmu.de> wrote:
>
>>
>> Dear All,
>> thank you very much for your help again. I tried now most of the
>> different proposals for importing the ascii grids & calculating the
>> temporal mean. All proposals are more elegant than my script, but
>> unfortunately the "stacking" and "bricking" is much more time and
>> computational demanding. I made some kind of benchmark testing. The
>> result is, that my not so elegant script is doing the job within 1minute
>> & 20 sec., while if I use stacking or bricking the best results are
>> about 10-12 minutes (for 360 Ascii Grids; 12* 30 years). So, for now I
>> will go on using my "old" script.
>>
>> Best,
>> Swen
>>
>>
>>        [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>

	[[alternative HTML version deleted]]

