Pacotes utilizados neste documento:
library(Rcpp)
library(tidyr)
library(dplyr, warn.conflicts = FALSE)
library(ggplot2)
library(microbenchmark)
C++ usando o pacote “Rcpp”.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector rowSumsC(NumericMatrix x) {
int nrow = x.nrow(), ncol = x.ncol();
NumericVector out(nrow);
for (int i = 0; i < nrow; i++) {
double total = 0;
for (int j = 0; j < ncol; j++) {
total += x(i, j);
}
out[i] = total;
}
return out;
}
cppSum <- function(big.matrix, N) {
stopifnot(exists("big.matrix"))
stopifnot(exists("N"))
Time <- microbenchmark({
colsums <- rowSumsC(big.matrix)
}, times = N)$time
return(Time)
}
Usando a função otimizada “colSums” do R:
ColSum <- function(big.matrix, N) {
stopifnot(exists("big.matrix"))
stopifnot(exists("N"))
Time <- microbenchmark({
colsums <- colSums(big.matrix)
}, times = N)$time
return(Time)
}
Usando função “apply” nativa do R:
Apply <- function(big.matrix, N) {
stopifnot(exists("big.matrix"))
stopifnot(exists("N"))
Time <- microbenchmark({
colsums <- apply(big.matrix, 2, sum)
}, times = N)$time
return(Time)
}
Usando multiplicação matricial:
matrixSum <- function(big.matrix, N) {
stopifnot(exists("big.matrix"))
stopifnot(exists("N"))
Time <- microbenchmark({
ones <- rep(1, dim(big.matrix)[1])
colsums <- ones %*% big.matrix
}, times = N)$time
return(Time)
}
Função com Laço simple:
SingleLoop <- function(big.matrix, N) {
stopifnot(exists("big.matrix"))
stopifnot(exists("N"))
colsums <- rep(NA, dim(big.matrix)[2])
Time <- microbenchmark({
for (i in 1:dim(big.matrix)[2]){
colsums[i] <- sum(big.matrix[,i])
}
}, times = N)$time
return(Time)
}
Função com laço duplo:
DoubleLoop <- function(big.matrix, N) {
stopifnot(exists("big.matrix"))
stopifnot(exists("N"))
Time <- microbenchmark({
colsums <- rep(NA, dim(big.matrix)[2])
for (i in 1:dim(big.matrix)[2]){
s <- 0
for (j in 1:dim(big.matrix)[1]){
s <- s + big.matrix[j, i]
}
colsums[i] <- s
}
}, times = N)$time
return(Time)
}
Trials = 100 # Number of experiments
Experiments <- as.data.frame(matrix(NA, nrow= Trials, ncol=5))
colnames(Experiments)<-c("CppSum", "SingleLoop", "Apply", "ColSum", "MatrixSum")
for (n in 1:Trials) {
big.matrix <- matrix(runif(1e+6), nrow = 1000)
Experiments[n,] = c(cppSum(big.matrix, N = 1),
SingleLoop(big.matrix, N = 1),
Apply(big.matrix, N = 1),
ColSum(big.matrix, N = 1),
matrixSum(big.matrix, N = 1))
}
p <- Experiments %>% gather("Método", "Tempo") %>% ggplot(aes(x = reorder(Método, Tempo, FUN=median), y = Tempo))
p + geom_boxplot() + labs(x = "Método", y = "Tempo em nanosegundos")
Trials = 10 # Number of experiments
Sizes = c(1000, 2000, 4000, 6000)
ExperimentsSizes <- as.data.frame(matrix(NA, nrow= Trials*length(Sizes), ncol=6))
colnames(ExperimentsSizes)<-c("CppSum", "SingleLoop", "Apply", "ColSum", "MatrixSum", "Size")
i <- 1
for (s in Sizes){
for (n in 1:Trials) {
big.matrix <- matrix(runif(s*s), nrow = s)
ExperimentsSizes[i,] = c(cppSum(big.matrix, N = 1),
SingleLoop(big.matrix, N = 1),
Apply(big.matrix, N = 1),
ColSum(big.matrix, N = 1),
matrixSum(big.matrix, N = 1),
s)
i <- i + 1
}
}
p<- ExperimentsSizes %>% gather("Método", "Tempo", -Size) %>% ggplot(aes(x = factor(Size), y = Tempo, fill = reorder(factor(Método), Tempo, FUN=median)))
p + geom_boxplot() + labs(fill = "Método", x = "Tamanho da Matriz", y = "Tempo em nanosegundos")