Skip to content

Commit

Permalink
As sent to Paul and Rob
Browse files Browse the repository at this point in the history
  • Loading branch information
aphalo committed Jan 7, 2024
1 parent 6eca12d commit 891eece
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 126 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion appendixes.prj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TeX:RNW:UNIX
17936379 0 -1 56245 -1 56250 342 342 1328 1400 1 1 952 580 -1 -1 0 0 31 -1 -1 31 1 0 56250 -1 0 -1 0
R.plotting.Rnw
TeX:RNW:UNIX
286371835 0 -1 232100 -1 232100 380 380 1366 1438 1 1 315 667 -1 -1 0 0 114715 -1 -1 114715 1 0 232100 -1 0 -1 0
286371835 0 -1 232100 -1 232100 380 380 1366 1438 1 1 354 3886 -1 -1 0 0 114715 -1 -1 114715 1 0 232100 -1 0 -1 0
references.bib
BibTeX:UNIX
1147890 0 477 48 477 44 76 76 1467 997 1 1 705 899 -1 -1 0 0 1 1 1 1 1 0 44 477 0 -1 0
Expand Down
2 changes: 1 addition & 1 deletion appendixes.prj.bak
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TeX:RNW:UNIX
17936379 0 -1 56245 -1 56250 342 342 1328 1400 1 1 952 580 -1 -1 0 0 31 -1 -1 31 1 0 56250 -1 0 -1 0
R.plotting.Rnw
TeX:RNW:UNIX
286371835 0 -1 232114 -1 232115 380 380 1366 1438 1 1 510 667 -1 -1 0 0 114715 -1 -1 114715 1 0 232115 -1 0 -1 0
286371835 0 -1 232100 -1 232100 380 380 1366 1438 1 1 354 667 -1 -1 0 0 114715 -1 -1 114715 1 0 232100 -1 0 -1 0
references.bib
BibTeX:UNIX
1147890 0 477 48 477 44 76 76 1467 997 1 1 705 899 -1 -1 0 0 1 1 1 1 1 0 44 477 0 -1 0
Expand Down
131 changes: 8 additions & 123 deletions benchmark-row-sums.R
Original file line number Diff line number Diff line change
@@ -1,124 +1,3 @@
<<<<<<< Updated upstream
library(microbenchmark)
library(ggplot2)
library(patchwork)

powers <- c(2, 4, 6, 8) # square matrices

benchmarks <- list()

for (i in powers) {
cat("Doing", i, "...")
gc()

num_rows <- 10^(i %/% 2)
A <- matrix(rnorm(10^i), nrow = num_rows)

benchmarks[[paste("dimension",
paste(dim(A), collapse = "*"),
sep = " ")]] <-
microbenchmark(
{
row.sum <- numeric()
for (i in 1:nrow(A)) {
row.sum[i] <- 0
for (j in 1:ncol(A))
row.sum[i] <- row.sum[i] + A[i, j]
}
},
{
row.sum <- numeric(nrow(A))
for (i in 1:nrow(A)) {
row.sum[i] <- 0
for (j in 1:ncol(A))
row.sum[i] <- row.sum[i] + A[i, j]
}
},
{
row.sum <- numeric()
for (i in 1:nrow(A)) {
row.sum[i] <- sum(A[i, ])
}
},
{
row.sum <- numeric(nrow(A))
for (i in 1:nrow(A)) {
row.sum[i] <- sum(A[i, ])
}
},
{
row.sum <- apply(A, MARGIN = 1, sum)
},
{
row.sum <- rowSums(A)
},
times = c(300L, 300L, 300L, 200L, 100L, 50L, 25L, 12L)[i]
)
cat(" done\n")
}

summaries <- data.frame()
for (b in benchmarks) {
summaries <-
rbind(summaries,
summary(b, unit = "millisecond")[ , c("expr", "median", "cld")])
}
summaries
summaries$size <- rep(10^powers, each = 6)
summaries$loop <- summaries$expr
levels(summaries$loop) <- c("nested for", "nested for alloc.",
"for and sum", "for and sum alloc.",
"apply and sum", "rowSums")

colnames(summaries)

fig.seconds <-
ggplot(summaries, aes(size, median*1e-3, color = loop)) +
geom_point() +
geom_line() +
scale_x_log10(name = "Vector length") +
scale_y_log10(name = "Time (s)") +
scale_color_discrete(name = "Iteration\napproach") +
theme_bw() # + theme(legend.position = "top")
fig.seconds

rel.summaries <- data.frame()
for (b in benchmarks) {
rel.summaries <-
rbind(rel.summaries,
summary(b, unit = "relative")[ , c("expr", "median", "cld")])
}
rel.summaries
rel.summaries$size <- rep(10^powers, each = 6)
rel.summaries$loop <- rel.summaries$expr
levels(rel.summaries$loop) <- c("nested for", "nested for alloc.",
"for and sum", "for and sum alloc.",
"apply and sum", "rowSums")

colnames(rel.summaries)

fig.rel <-
ggplot(rel.summaries, aes(size, median, color = loop)) +
geom_point() +
geom_line() +
scale_x_log10(name = "Vector length") +
scale_y_log10(name = "Time (relative to shortest)",
breaks = c(1, 2, 5, 10, 20, 50, 100, 200, 500, 1000)) +
scale_color_discrete(name = "Iteration\napproach") +
theme_bw() # + theme(legend.position = "none")
fig.rel

diff.benchmark.fig <-
fig.seconds / fig.rel + plot_layout(guides = "collect")
diff.benchmark.fig

save(diff.benchmark.fig,
fig.seconds,
fig.rel,
summaries,
rel.summaries,
file = "benchmarks-rowSums-pantera.Rda")
=======
library(microbenchmark)
library(ggplot2)
library(patchwork)
Expand Down Expand Up @@ -232,10 +111,16 @@ diff.benchmark.fig <-
fig.seconds / fig.rel + plot_layout(guides = "collect")
diff.benchmark.fig

# save(diff.benchmark.fig,
# fig.seconds,
# fig.rel,
# summaries,
# rel.summaries,
# file = "benchmarks-rowSums-pantera.Rda")

save(diff.benchmark.fig,
fig.seconds,
fig.rel,
summaries,
rel.summaries,
file = "benchmarks-rowSums-pantera.Rda")
>>>>>>> Stashed changes
file = "benchmarks-rowSums-angus.Rda")
9 changes: 8 additions & 1 deletion benchmark-vector-diff.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,11 @@ save(diff.benchmark.fig,
fig.rel,
summaries,
rel.summaries,
file = "benchmarks.carbonilla.Rda")
file = "benchmarks.angus.Rda")

# save(diff.benchmark.fig,
# fig.seconds,
# fig.rel,
# summaries,
# rel.summaries,
# file = "benchmarks.carbonilla.Rda")

0 comments on commit 891eece

Please sign in to comment.