These are suggestions from docs.julialang.org
@time
¶@time
reports time as well as memory allocation@time
vs. functions like tic
and toc
function fib(n)
n < 2 ? n : fib(n-1) + fib(n-2)
end
fib (generic function with 1 method)
fib <- function(n) {
if(n < 2) {
return(n)
} else {
return(fib(n-1) + fib(n-2))
}
}
function array_construct(n)
ones(n, n)
end
array_construct (generic function with 1 method)
matrix(1, 200, 200)
function mat_mult(n)
A = array_construct(n);
A*A'
end
mat_mult (generic function with 1 method)
A <- matrix(1, 200, 200)
A%*%t(A)
function inner(x, y)
result = 0.0
for i=1:length(x)
result += x[i] * y[i]
end
result
end
inner (generic function with 1 method)
x <- rnorm(10000000)
y <- rnorm(10000000)
x %*% y
function rand_mat_stat(t)
n = 5
v = zeros(t)
w = zeros(t)
for i=1:t
a = randn(n,n)
b = randn(n,n)
c = randn(n,n)
d = randn(n,n)
P = [a b c d]
Q = [a b; c d]
v[i] = trace((P.'*P)^4)
w[i] = trace((Q.'*Q)^4)
end
return (std(v)/mean(v), std(w)/mean(w))
end
rand_mat_stat (generic function with 1 method)
rand_mat_stat <- function(t) {
n <- 5
stuff <- sapply(1:t, function(i){
mat_a <- matrix(rnorm(n*n), n, n)
mat_b <- matrix(rnorm(n*n), n, n)
mat_c <- matrix(rnorm(n*n), n, n)
mat_d <- matrix(rnorm(n*n), n, n)
P <-cbind(mat_a, mat_b, mat_c, mat_d)
Q <- rbind(cbind(mat_a, mat_b),cbind(mat_c, mat_d))
c(sum(diag((t(P)%*%P)^4)), sum(diag((t(Q)%*%Q)^4)))
})
res <- apply(stuff,1,function(x) sd(x)/mean(x))
return(res)
}
library(ggplot2)
times <- read.csv("written_data/combined_benchmark_times.csv")
qplot(expr, time, data=times, geom="boxplot", colour=lang)
inner(x, y)
¶times_inner <- read.csv("written_data/combined_benchmark_inner.csv")
qplot(as.factor(n), time, data=times_inner, geom="boxplot", colour=lang)