bench icon indicating copy to clipboard operation
bench copied to clipboard

Better breaks for scale_*_bench_time()

Open paleolimbot opened this issue 5 years ago • 2 comments

@hadley suggested that perhaps your vroom benchmarks could have nicer breaks for the values that aren't in seconds. This is a quick fix that uses the excellent rounding and parsing code for format.bench_time() and as_bench_time.default():

bench_time_breaks <- function(n = 5, base = 10, digits = 0) {
  force(digits)
  log_breaks <- scales::log_breaks(n = n, base = base)
  function(x) {
    log_breaks_secs <- scales::log_breaks(n = n, base = base)(x)
    bench_time_formatted <- bench:::format.bench_time(log_breaks_secs, digits = digits, trim = TRUE)
    bench:::as_bench_time.default(bench_time_formatted)
  }
}

library(ggplot2)
df <- tibble::tibble(x = 10^((-6):4))
p <- ggplot(df, aes(x, 1)) + geom_point()  

p + bench:::scale_x_bench_time(labels = bench:::format.bench_time)

p + bench:::scale_x_bench_time(breaks = bench_time_breaks(), labels = bench:::format.bench_time)

Created on 2019-06-17 by the reprex package (v0.2.1)

I imagine that benchmarking things that are longer than a minute are uncommon...we will probably use some version of this for a set of scale_*_difftime() functions in scales/ggplot2 at some point.

paleolimbot avatar Jun 17 '19 19:06 paleolimbot

Agreed that would be nice, looks good!

The vroom benchmarks actually aren't using scale_x_bench_time, they are just using scale_y_continuous(labels = function(x) format(bench::as_bench_time(x))), but nicer breaks would definitely be nice!

jimhester avatar Jun 18 '19 13:06 jimhester

For posterity,

bench_time_breaks <- function(breaks = scales::extended_breaks(), digits = 0) {
  force(breaks)
  force(digits)
  function(x) {
    log_breaks_secs <- breaks(x)
    bench_time_formatted <- bench:::format.bench_time(log_breaks_secs, digits = digits, trim = TRUE)
    bench:::as_bench_time.default(bench_time_formatted)
  }
}

library(ggplot2)
df <- tibble::tibble(x = c(2, 10*60))
p <- ggplot(df, aes(x, 1)) + geom_point()  

p + scale_x_continuous(labels = bench:::format.bench_time, breaks = bench_time_breaks())

Created on 2019-06-18 by the reprex package (v0.2.1)

paleolimbot avatar Jun 18 '19 13:06 paleolimbot