Skip to content

Commit

Permalink
Chapter 7 review (csgillespie#161)
Browse files Browse the repository at this point in the history
* Addressed reviewers comments.

* Use devtools version of package

* Trying to fix travis error

* Tracking down travis

* Still git dramas

* Bump Rcpp version

* Clear Glob Env
  • Loading branch information
csgillespie authored and Robinlovelace committed Sep 23, 2016
1 parent 7b3aa29 commit 8734c04
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 340 deletions.
539 changes: 307 additions & 232 deletions 07-performance.Rmd

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Depends: assertive.reflection, benchmarkme, bookdown, cranlogs, data.table (>= 1
devtools (>= 1.12.0), DiagrammeR, dplyr, drat, efficient, formatR, fortunes,
geosphere, ggmap, ggplot2 (>= 2.1.0), ggplot2movies, knitr, lubridate,
microbenchmark, profvis (>= 0.3.2), pryr, readr, RSQLite, tibble (>= 1.1.0), tidyr,
Rcpp (>= 0.12.6)
Rcpp (>= 0.12.7)
LazyData: TRUE
LinkingTo: Rcpp
Remotes:
Expand Down
41 changes: 0 additions & 41 deletions code/07-performance_f2b.R

This file was deleted.

78 changes: 30 additions & 48 deletions code/07-performance_f3.R
Original file line number Diff line number Diff line change
@@ -1,59 +1,41 @@
source("code/initialise.R")

# # Code to download logs of various packages
# # Not run to avoid cranlogs dependency for book
# dd = cranlogs::cran_downloads(packages = c("V8", "Rcpp", "rPython", "rJava"),
# from = "2013-01-01", to = "2016-09-01")
# dd$Downloads <- ave(
# dd$count,
# dd$package,
# FUN = function(x)
# zoo::rollmean(x, k = 30, na.pad = T)
# )
# saveRDS(dd, "extdata/cranlog.Rds")

dd = readRDS("extdata/cranlog.Rds")
dd = dd[dd$count > 0, ]

v8 = dd[dd$package=="V8",]
rcpp = dd[dd$package=="Rcpp",]
rjava = dd[dd$package=="rJava",]
rpython = dd[dd$package=="rPython",]
# ## Original idea from https://github.com/csgillespie/efficientR/issues/121 and @adamryczkowski
#
# n_rep=10 #Number of times to generate a test sample. This smooths the chart.
# dd = matrix(0, nrow=100, ncol=3)
# ns = 10^(seq(from=log10(10), to=log10(10^6), length.out =nrow(dd)))
#
# tmp = matrix(0, nrow=n_rep, ncol=2)
# for (i in seq_len(nrow(dd)))
# {
# for(j in seq_len(n_rep))
# {
# s = sample(c(TRUE, FALSE), ns[i], TRUE, prob=c(1-0.0001,0.0001))
# tmp[j,] = summary(
# microbenchmark::microbenchmark(
# which.max(s),
# which(s)[[1]])
# )[['median']]
# }
# dd[i,] = c(ns[i], colMeans(tmp))
# cat(".")
# }
#saveRDS(dd, file="extdata/07-which_comparison.RData")
dd = readRDS(file="extdata/07-which_comparison.RData")

par(mar=c(3,3,2,1), mgp=c(2,0.4,0), tck=-.01,
cex.axis=0.9, las=1, xaxs='i',yaxs='i')

# Blank graph
start = as.Date("2013-01-01"); end = as.Date("2016-09-01")
plot(rcpp$date[1], type="n", xlim=c(start, end), ylim=c(10^0, 2*10^4),
plot(dd[,1], dd[,3]/dd[,2], log="xy",
pch=21, xlim=c(9, 10^4), ylim=c(1, 10^2),
axes=FALSE, frame=FALSE,
xlab="Time", ylab="Downloads per day",
log="y")
abline(h=10^(0:4), lty=3, col="grey80")

# Add R versions
R_dates = as.Date(c("2013-04-03", "2014-04-03", "2015-04-03", "2016-04-03"))
R_vers = paste0("R 3.", 0:3)
abline(v=R_dates, col="grey90", lty=2)
text(R_dates, 11800, R_vers, pos=4, col="grey50")
ylab="Relative speed of 'which.min' compared to 'which'", xlab="Vector length",
bg="steelblue")
abline(h=10^(0:2), lty=3, col="grey80")
abline(h=c(2, 5, 20, 50), lty=3, col="grey90")

# Label lines
lines(rcpp$date, rcpp$Downloads, lwd=3,col=3)
lines(v8$date, v8$Downloads, lwd=2, col=4)
lines(rjava$date, rjava$Downloads, lwd=2, col=2)
lines(rpython$date, rpython$Downloads, lwd=2, col=1)

# Add axis
axis(2, tick=FALSE)
axis(1, at = c(as.Date("2013-01-01"),as.Date("2014-01-01"),as.Date("2015-01-01"), as.Date("2016-01-01")),
labels=2013:2016, tick=F)
axis(1, at = 10^(1:4), labels=c(expression(10^1),expression(10^2),expression(10^3),expression(10^4)), tick=F)

title("The rise of Rcpp", adj=1,
cex.main=1, font.main=2, col.main="black")

# Label lines
text(as.Date("2013-06-01"), 6, "rPython", col=1, font = 2)
text(as.Date("2014-02-01"), 3000, "Rcpp", col=3, font = 2)
text(as.Date("2015-02-01"), 80, "V8", col=4, font = 2)
text(as.Date("2015-03-01"), 1000, "rJava", col=2, font = 2)

59 changes: 59 additions & 0 deletions code/07-performance_f5.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
source("code/initialise.R")

# # Code to download logs of various packages
# # Not run to avoid cranlogs dependency for book
# dd = cranlogs::cran_downloads(packages = c("V8", "Rcpp", "rPython", "rJava"),
# from = "2013-01-01", to = "2016-09-01")
# dd$Downloads <- ave(
# dd$count,
# dd$package,
# FUN = function(x)
# zoo::rollmean(x, k = 30, na.pad = T)
# )
# saveRDS(dd, "extdata/cranlog.Rds")

dd = readRDS("extdata/cranlog.Rds")
dd = dd[dd$count > 0, ]

v8 = dd[dd$package=="V8",]
rcpp = dd[dd$package=="Rcpp",]
rjava = dd[dd$package=="rJava",]
rpython = dd[dd$package=="rPython",]

par(mar=c(3,3,2,1), mgp=c(2,0.4,0), tck=-.01,
cex.axis=0.9, las=1, xaxs='i',yaxs='i')

# Blank graph
start = as.Date("2013-01-01"); end = as.Date("2016-09-01")
plot(rcpp$date[1], type="n", xlim=c(start, end), ylim=c(10^0, 2*10^4),
axes=FALSE, frame=FALSE,
xlab="Time", ylab="Downloads per day",
log="y")
abline(h=10^(0:4), lty=3, col="grey80")

# Add R versions
R_dates = as.Date(c("2013-04-03", "2014-04-03", "2015-04-03", "2016-04-03"))
R_vers = paste0("R 3.", 0:3)
abline(v=R_dates, col="grey90", lty=2)
text(R_dates, 11800, R_vers, pos=4, col="grey50")

# Label lines
lines(rcpp$date, rcpp$Downloads, lwd=3,col=3)
lines(v8$date, v8$Downloads, lwd=2, col=4)
lines(rjava$date, rjava$Downloads, lwd=2, col=2)
lines(rpython$date, rpython$Downloads, lwd=2, col=1)

# Add axis
axis(2, tick=FALSE)
axis(1, at = c(as.Date("2013-01-01"),as.Date("2014-01-01"),as.Date("2015-01-01"), as.Date("2016-01-01")),
labels=2013:2016, tick=F)

title("The rise of Rcpp", adj=1,
cex.main=1, font.main=2, col.main="black")

# Label lines
text(as.Date("2013-06-01"), 6, "rPython", col=1, font = 2)
text(as.Date("2014-02-01"), 3000, "Rcpp", col=3, font = 2)
text(as.Date("2015-02-01"), 80, "V8", col=4, font = 2)
text(as.Date("2015-03-01"), 1000, "rJava", col=2, font = 2)

30 changes: 15 additions & 15 deletions code/07-performance_f4.R → code/07-performance_f6.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ z$expr = factor(z$expr, levels=c("mean(x)", "mean_c(x)", "com_mean_r(x)", "mean
z$expr = factor(z$expr, labels=c("Base", "Rcpp", "Byte compiled R", "Pure R"))

z$time = z$time/10^6
g = ggplot(z) +
geom_violin(aes(expr, time),position=position_dodge(0.9), bg=get_col(3)) +
scale_y_continuous(limits=c(10^-3, 10^1), expand=c(0, 0), breaks = 10^(-3:1),
trans="log10",
g = ggplot(z) +
geom_violin(aes(expr, time),position=position_dodge(0.9), bg=get_col(3)) +
scale_y_continuous(limits=c(10^-3, 10^1), expand=c(0, 0), breaks = 10^(-3:1),
trans="log10",
labels=c(expression(10^-3),expression(10^-2),
expression(10^-1),expression(10^0),expression(10^1)))

g1 = g + labs(title = "Performance Gains with Rcpp",
x = NULL, y = "Elapsed Time (secs)",colour = NULL, fill = NULL) +
theme(panel.grid.major.y = element_line(colour = "gray90"),
panel.grid.minor = element_line(colour = NA),
panel.grid.major.x = element_line(colour = NA),
plot.title = element_text(size = 12,
face = "bold", hjust = 1, vjust = 0),
panel.background = element_rect(fill = NA),
legend.background = element_rect(fill = NA),
legend.position = c(0.93, 0.92),
g1 = g + labs(title = "Performance Gains with Rcpp",
x = NULL, y = "Elapsed Time (secs)",colour = NULL, fill = NULL) +
theme(panel.grid.major.y = element_line(colour = "gray90"),
panel.grid.minor = element_line(colour = NA),
panel.grid.major.x = element_line(colour = NA),
plot.title = element_text(size = 12,
face = "bold", hjust = 1, vjust = 0),
panel.background = element_rect(fill = NA),
legend.background = element_rect(fill = NA),
legend.position = c(0.93, 0.92),
axis.ticks.x = element_line(linetype = "blank"),
axis.ticks.y = element_line(linetype = "blank"))
print(g1)
print(g1)
Binary file modified figures/f7_2_profvis_monopoly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added figures/f7_4_profvis_monopoly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/mean_c.cpp → src/mean_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
using namespace Rcpp;

// [[Rcpp::export]]
double mean_c(NumericVector x){
double mean_cpp(NumericVector x){
int i;
int n = x.size();
double mean = 0;

for(i=0; i<n; i++){
mean = mean + x[i]/n;
}
return mean;
}

// [[Rcpp::export]]
NumericVector res_c(NumericVector x, NumericVector y){
NumericVector res_cpp(NumericVector x, NumericVector y){
int i;
int n = x.size();
NumericVector residuals(n);
Expand Down

0 comments on commit 8734c04

Please sign in to comment.