Log Lines for Classes 5 and 6, STAT 705 Fall 2017 ================================================= 9/13/2017 #### "sum" or "max" or "quantile" have argument na.rm (default F) ### Timing run rowsums = array(0, c(1e5,100)) Xmat = array(runif(1e7), c(1e5,1e2)) #### Note that the random-number generation ### is now outside the timing runs. ones = rep(1,100) system.time({ for(j in 1:100){ for(i in 1:1e5) rowsums[i,j] = sum(Xmat[i,]) } }) user system elapsed 10.54 0.00 10.55 system.time({ for(j in 1:100){ rowsums[,j] = c(Xmat %*% ones) } }) user system elapsed 1.32 0.04 1.36 #### OK, so without the random-number generation the ### actual computation of rowsums was 8 times as fast via linear algebra #### now try this with other indexing parameters: colsums = array(0, c(100,100)) ones2 = rep(1,1e5) system.time({ for(j in 1:100){ for(i in 1:1e2) colsums[i,j] = sum(Xmat[,i]) } }) user system elapsed 5.61 2.13 7.74 system.time({ for(j in 1:100){ colsums[,j] = c(ones2 %*% Xmat) } }) user system elapsed 1.60 0.00 1.59 #### OK, without the random-number generation the ### actual computation of colsums was 4 times as fast via linear algebra ##### SO IT IS A BIT QUICKER TO DO DO LINEAR ALGEBRA THAN FOR-LOOPS, ##### BUT THE COMPARISON IS FAR LESS UNBALANCED THAN IN EARLIER VERSIONS OF R ##------------------------------------------------------------ ## See Logs at OldLogs/F16Logs, specifically Sep12F16.RLog and Sep14F16.RLog ## Compare the latter with: > g = function(x,b) 2+7*x-b*x^2 fmax2B = function(b) { bopt = optimize(g, c(0,10), maximum=T) unlist(bopt) } > fmax2B(3) Error in f(arg, ...) : argument "b" is missing, with no default > b=3 fmax2B(5) > fmax2B(5) Error in f(arg, ...) : argument "b" is missing, with no default