Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New function to benchmark components of plotting

  • Loading branch information...
commit a8b56fd752e9c5c186e1135c2700550560001cbc 1 parent 2ff9003
@hadley authored
View
1  DESCRIPTION
@@ -41,6 +41,7 @@ Collate:
'aaa-html.r'
'aes.r'
'annotation.r'
+ 'bench.r'
'coord-.r'
'coord-cartesian-.r'
'coord-cartesian-equal.r'
View
1  NAMESPACE
@@ -7,6 +7,7 @@ export(aes)
export(aes_string)
export(aes_all)
export(annotate)
+export(benchplot)
export(coord_cartesian)
export(coord_fixed, coord_equal)
export(coord_flip)
View
24 R/bench.r
@@ -0,0 +1,24 @@
+#' Benchmark plot creation time.
+#' Broken down into construct, build, render and draw times.
+#'
+#' @param x code to create ggplot2 plot
+#' @export
+#' @keywords internal
+#' @examples
+#' benchplot(qplot(mpg, wt, data = mtcars))
+#' benchplot(qplot(mpg, wt, data = mtcars) + facet_grid(.~ cyl))
+benchplot <- function(x) {
+
+ construct <- system.time(force(x))
+ stopifnot(inherits(x, "ggplot"))
+
+ build <- system.time(data <- ggplot_build(x))
+ render <- system.time(grob <- ggplotGrob(x, data))
+ draw <- system.time(grid.draw(grob))
+
+ times <- rbind(construct, build, render, draw)[, 1:3]
+
+ unrowname(data.frame(
+ step = c("construct", "build", "render", "draw", "TOTAL"),
+ rbind(times, colSums(times))))
+}
View
3  R/plot-render.r
@@ -26,10 +26,9 @@ panelGrob <- function(panels, plot, data) {
# This function sets up the appropriate viewports and packs the
# various components in. The viewport is set up so that each component
# will only take up the amount of space that it requires.
-ggplotGrob <- function(plot, drop = plot$options$drop, keep = plot$options$keep, ...) {
+ggplotGrob <- function(plot, data = ggplot_build(plot), drop = plot$options$drop, keep = plot$options$keep, ...) {
plot <- plot_clone(plot)
- data <- ggplot_build(plot)
grobs <- plot$facet$make_grobs(data, plot$layers, plot$coordinates)
grobs3d <- array(unlist(grobs, recursive=FALSE), c(dim(data[[1]]), length(data)))
View
15 man/benchplot.Rd
@@ -0,0 +1,15 @@
+\name{benchplot}
+\alias{benchplot}
+\title{Benchmark plot creation time.}
+\usage{benchplot(x)}
+
+\description{
+ Benchmark plot creation time. Broken down into construct,
+ build, render and draw times.
+}
+\keyword{internal}
+\arguments{
+ \item{x}{code to create ggplot2 plot}
+}
+\examples{benchplot(qplot(mpg, wt, data = mtcars))
+benchplot(qplot(mpg, wt, data = mtcars) + facet_grid(.~ cyl))}
Please sign in to comment.
Something went wrong with that request. Please try again.