From 891eece9154c9a28cfd35a732b1e5a88f338bc7a Mon Sep 17 00:00:00 2001 From: Pedro Aphalo Date: Sun, 7 Jan 2024 16:31:17 +0200 Subject: [PATCH] As sent to Paul and Rob --- ...pdf => Aphalo-CR-9781032518435-Learn-R.pdf | Bin appendixes.prj | 2 +- appendixes.prj.bak | 2 +- benchmark-row-sums.R | 131 ++---------------- benchmark-vector-diff.R | 9 +- 5 files changed, 18 insertions(+), 126 deletions(-) rename using-r-main-crc.pdf => Aphalo-CR-9781032518435-Learn-R.pdf (100%) diff --git a/using-r-main-crc.pdf b/Aphalo-CR-9781032518435-Learn-R.pdf similarity index 100% rename from using-r-main-crc.pdf rename to Aphalo-CR-9781032518435-Learn-R.pdf diff --git a/appendixes.prj b/appendixes.prj index e2c45800..0218d546 100644 --- a/appendixes.prj +++ b/appendixes.prj @@ -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 diff --git a/appendixes.prj.bak b/appendixes.prj.bak index 8e48a348..40134d03 100644 --- a/appendixes.prj.bak +++ b/appendixes.prj.bak @@ -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 diff --git a/benchmark-row-sums.R b/benchmark-row-sums.R index 5b121c4b..a9c08b6d 100644 --- a/benchmark-row-sums.R +++ b/benchmark-row-sums.R @@ -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) @@ -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") diff --git a/benchmark-vector-diff.R b/benchmark-vector-diff.R index 29218dae..339e8e63 100644 --- a/benchmark-vector-diff.R +++ b/benchmark-vector-diff.R @@ -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")