开发者

How to make a plot from summaryRprof?

开发者 https://www.devze.com 2023-02-04 16:12 出处:网络
This is a question for an university assignment. I was given three algorithms to calculate the GCD that I already did. My problem is getting the Rprof results to a plot so I can compare them side by

This is a question for an university assignment.

I was given three algorithms to calculate the GCD that I already did. My problem is getting the Rprof results to a plot so I can compare them side by side.

From what little understanding I have about Rprof, summaryRprof and plot is that Rprof is used like this:

Rprof() #To start
#functions here
Rprof(NULL)  #TO end
summaryRprof() # to print results

I understand that plot has many different types of inputs, x and y values and somet开发者_如何学JAVAhing called a data frame which I assume is a fancy word for table. and to draw different lines and things I need to use this: http://www.harding.edu/fmccown/r/

what I cant figure out is how to get the summaryRprof results to the plot() function.

> Rprof(filename="RProfOut2.out", interval=0.0001)
> gcdBruteForce(10000, 33)
[1] 1
> gcdEuclid(10000, 33)
[1] 1
> gcdPrimeFact(10000, 33)
[1] 1
> Rprof(NULL)
> summaryRprof()
?????plot????

I have been reading on stack overflow that and other sites that I can also try to use profr and proftools although I am not very clear on the usage.

The only graph I have been able to make is one using plot(system.time(gcdFunction(10,100)))

As always any help is appreciated.


There are two packages that visualize Rprof output:

  • profr by Hadley Wickham

  • proftools by Luke Tierney

On a Unix/OS X system with graphviz installed you can use a nice Perl script by Romain Francois that is on the R Wiki page on profiling.

All this is described with examples in my Intro to HPC with R tutorials you can find e.g. here.


The gcd functions must be something you've coded separately as they aren't a part of base R. Here is a contrived example to show you how to get the data into a usable format for further processing and plotting.

First, you need to pass summaryRprof() a filename to process. In your example, this would be summaryRprof("RProfOut2.out").

This will return the summary statistics for your previous code. Since we need to do further processing on these statistics, let's assign it to a new object:

sumStats <- summaryRprof("RProfOut2.out")

This returns a list object with 4 elements:

> str(sumStats)
List of 4
 $ by.self        :'data.frame':    2 obs. of  4 variables:
  ..$ self.time : num [1:2] 1.97 0.25
  ..$ self.pct  : num [1:2] 88.7 11.3
  ..$ total.time: num [1:2] 1.97 0.25
  ..$ total.pct : num [1:2] 88.7 11.3
 $ by.total       :'data.frame':    3 obs. of  4 variables:
  ..$ total.time: num [1:3] 1.97 0.25 0
  ..$ total.pct : num [1:3] 88.7 11.3 0
  ..$ self.time : num [1:3] 1.97 0.25 0
  ..$ self.pct  : num [1:3] 88.7 11.3 0
 $ sample.interval: num 1e-04
 $ sampling.time  : num 2.22

At this point, I'm assuming you are going to be interested in one of the first two data.frames. I prefer the graphics in ggplot2 over base graphics, but you can certainly achieve most things with base graphics...I just have more experience with ggplot2. Here's one approach to plotting the data for the by.self() dataframe that was generated:

require(ggplot2)

byself <- sumStats$by.self

byself$functions <- rownames(byself)

m <- melt(byself, id.var = "functions")

qplot(functions, value, data = m, fill = variable, geom = "bar", position = "dodge")

That makes a plot that looks like this:

How to make a plot from summaryRprof?

And for completeness sake, here is the contrived code I made up for the example. I know not very creative, but gets the job done:

Rprof("Rprof.out", interval = 0.0001)
x <- rnorm(10000000)
y <- x ^ 2
Rprof(NULL)
sumStats <- summaryRprof("Rprof.out")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号