##R version 3.4.2 (2017-09-28) ##Platform: x86_64-pc-linux-gnu (64-bit) ##Running under: Ubuntu 16.04.3 LTS bm_prog_toeplitz = function() { N = 3000 ans = rep(0, N*N) dim(ans) = c(N, N) system.time({ for (j in 1:N) { for (k in 1:N) { ans[k,j] = abs(j - k) + 1 } } } ) } ## Takes around 7 seconds bm_prog_toeplitz() ## Around 2 seconds N = 3000 ans = rep(0, N*N) dim(ans) = c(N, N) system.time({ for (j in 1:N) { for (k in 1:N) { ans[k,j] = abs(j - k) + 1 } } })