bench
bench copied to clipboard
Better breaks for scale_*_bench_time()
@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.
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!
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)