From c24e72080df2b70ff1581e00219f9a424c859a9c Mon Sep 17 00:00:00 2001 From: parmsam Date: Wed, 24 Apr 2024 14:35:44 -0400 Subject: [PATCH 1/2] add benchmarks vignette --- .gitignore | 1 + DESCRIPTION | 3 ++ vignettes/.gitignore | 2 ++ vignettes/performance.Rmd | 66 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 vignettes/.gitignore create mode 100644 vignettes/performance.Rmd diff --git a/.gitignore b/.gitignore index c8e84af..b07c3d3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ src/*.so src/*.dll scratch.R docs +inst/doc diff --git a/DESCRIPTION b/DESCRIPTION index 151c5f5..a99968e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,10 @@ LazyData: true LinkingTo: cpp11 Suggests: + knitr, + rmarkdown, testthat (>= 3.0.0) Config/testthat/edition: 3 RoxygenNote: 7.2.3 URL: https://parmsam.github.io/lzstring-r/ +VignetteBuilder: knitr diff --git a/vignettes/.gitignore b/vignettes/.gitignore new file mode 100644 index 0000000..097b241 --- /dev/null +++ b/vignettes/.gitignore @@ -0,0 +1,2 @@ +*.html +*.R diff --git a/vignettes/performance.Rmd b/vignettes/performance.Rmd new file mode 100644 index 0000000..6275c1c --- /dev/null +++ b/vignettes/performance.Rmd @@ -0,0 +1,66 @@ +--- +title: "Benchmarking performance" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{performance} + %\VignetteEncoding{UTF-8} + %\VignetteEngine{knitr::rmarkdown} +editor_options: + chunk_output_type: console +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` + +```{r setup} +library(lzstring) +library(bench) +library(ggplot2) +``` + +Let's benchmark use of lzstring for compression to base 64 and decompression from base 64. We will generate random strings of different lengths and measure the time taken for compression and decompression. + +```{r benchmark} +# Function to generate a random string of specified length +generate_random_string <- function(length) { + paste(sample(c(LETTERS, 0:9), size = length, replace = TRUE), collapse = "") +} + +# Prepare a data frame to store benchmark results +benchmark_results <- data.frame(length_str = numeric(), comp_time = numeric(), decomp_time = numeric()) + +# Run benchmark for each string length +for (i in 1:100) { + big_str <- generate_random_string(i * 100) + + comp <- mark( + compressToBase64(big_str) + ) + decomp <- mark( + decompressFromBase64(comp$result[[1]]) + ) + decomp_time_tmp <- as.numeric(decomp$median) + comp_time_tmp <- as.numeric(comp$median) + # Store the median time and string length + benchmark_results <- rbind(benchmark_results, data.frame(length_str = nchar(big_str), comp_time = comp_time_tmp, decomp_time = decomp_time_tmp)) +} +# Convert time to milliseconds +benchmark_results$comp_time <- benchmark_results$comp_time * 1000 +benchmark_results$decomp_time <- benchmark_results$decomp_time * 1000 +``` + +Now let's plot the results for compression and decompression times of all those strings we created. + +```{r plot-results, fig.width = 7, fig.asp = 0.6} +ggplot(benchmark_results) + + geom_line(aes(x = length_str, y = comp_time, col = "Compression"), ) + + geom_line(aes(x = length_str, y = decomp_time, col = "Decompression")) + + labs(x = "String length (characters)", + y = "Time (milliseconds)") + + theme_minimal() + + theme(legend.position = "bottom") +``` From 3b663d471d2e0e2ed5b2596b681cce13b8ce3b42 Mon Sep 17 00:00:00 2001 From: parmsam Date: Wed, 24 Apr 2024 14:49:04 -0400 Subject: [PATCH 2/2] ensure bench and ggplot2 are in suggests for vignette rendering --- DESCRIPTION | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index a99968e..5cd4a64 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,6 +14,8 @@ LazyData: true LinkingTo: cpp11 Suggests: + bench, + ggplot2, knitr, rmarkdown, testthat (>= 3.0.0)