Defining utility functions
burd = colorRampPalette(colors = c("blue", "white", "red"))(n = 499)
blues = colorRampPalette(colors = c('#deebf7', '#08306b'))(n = 256)
plot.matrix = function(m, col = burd, asp=1) {
m %>%
apply(MARGIN = 2, rev) %>%
t() %>%
image(useRaster = TRUE, axes = FALSE, col = col, asp = asp)
}
parse_timing_output = function(output_raw) {
sapply(output_raw, function(x) {
str = stringr::str_split(x,":\\s+")[[1]]
return(as.numeric(str[2]))
})
}
An arbitrary matrix
sin2d = function(a, b) {
sin((a/ 500 - b / 15) * pi)
}
start = proc.time()
m = outer(1:1000, 1:30, 'sin2d')
dt1 = (proc.time() - start)['elapsed']
plot.matrix(m, asp = 5/3)
Run R
start = proc.time()
r_svd = svd(m)
dt2 = proc.time() - start
bm = microbenchmark::microbenchmark(svd(m))
dt3 = mean(bm$time) * 1e-9
sprintf("Time to create the matrix : %s seconds", dt1['elapsed'])
## [1] "Time to create the matrix : 0.00499999999999989 seconds"
sprintf("Time to calculate SVD in R: %s seconds", dt2['elapsed'])
## [1] "Time to calculate SVD in R: 0.00800000000000001 seconds"
sprintf("Average time to calculate SVD in R: %s seconds", dt3)
## [1] "Average time to calculate SVD in R: 0.00341847058 seconds"
Run python
py_output = system('python3 ../../static/assets/src/multilang-svd/py-svd.py', intern = TRUE, ignore.stdout = FALSE, ignore.stderr = FALSE, wait = TRUE)
py_output
## [1] "Time to create the matrix (seconds): 0.035224"
## [2] "Time to calculate SVD in python (seconds): 0.071630"
## [3] "Average time to calculate SVD in python (seconds): 0.060274"
Run Julia
jl_output = system(sprintf('%s ../../static/assets/src/multilang-svd/jl-svd.jl', julia_exec), intern = TRUE, ignore.stdout = FALSE, ignore.stderr = FALSE, wait = TRUE)
jl_output
## [1] "Time to create the matrix (seconds): 0.404455179"
## [2] "Time to calculate SVD in the first run (seconds): 0.747887515"
## [3] "Time to calculate SVD in the second run (seconds): 0.001350468"
## [4] "Average time to calculate SVD (seconds) : 0.0056974489"
Summary
R |
R |
0.0034185 |
Python |
Python |
0.0602740 |
Julia |
Julia |
0.0056974 |