Performing several hundred raster multiplications
Rasters

Rasters are highly memory intensive in R's already fragile memory space. However, one does not necessarily need to read a raster into memory each time. In fact, it is highly frowned upon to read in a raster in that way as it just eats up so much memory and is computationally intensive.

stack()'s beauty

It's much better to make a stack of rasters as it does not load them into memory, but opens their file location.

library(raster)

A_Files &lt;- list.files(pattern="A.tif") # files A
All_A &lt;- stack(unique(A_Files))

B_Files &lt;- list.files(pattern="B") # files B
All_B &lt;- stack(unique(B_Files))


Notice, all of the elements are now in a "vectorized" form. 

overlay() to calculate

From here, we can use overlay() to perform a calculation. Note, since the number of layers is equivalent between (ALL_A) and (ALL_B) we do not need to worry about an element being repeated. 

x &lt;- overlay(All_A, All_B, fun=function(x,y) x*y )


Export

The only downside to this approach is you will have to go through a loop to export the answers: 

out &lt;- unstack(x)
outputnames &lt;- paste0(seq_along(out), "_result.tif")
for(i in seq_along(out)){ writeRaster(out[[i]], file=outputnames[i]) }


References

For multiplying stacks see:


overlay
calc


For more information on stacks (see bricks as well)


stack
brick